473,785 Members | 2,794 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting two variables from a PHP loop

Hi Everyone,
I have a huge problem. I am trying to get two corresponding variables
from a Checkbox and a Text box. e.g. If I check a value, say:
enrolled? [] (checkbox) then I have to put the date enrolled as a
text. So when inserting in a table I want the enrolled to correspond
to each date. Here is what I am trying but not working (In the echos
is where I am trying to have the values into the table):

while (list($key, $val) = @each ($box))
{
echo "$val";
while (list($key, $v_val) = @each ($dose))
{
echo "$v_val";
}
}
echo "<form method=post action=''>";
echo "<table border='0' cellspacing='0' style='border-collapse:
collapse' width='100' >

<tr bgcolor='white' >
<td width='25%'><in put type=checkbox name=box[] value='John'></td>
<td width='13%'>&nb sp;John</td>
<td width='12%'><in put type=text name=date[] size=8></td>
<td width='25%'><in put type=checkbox name=box[] value='Peter'></td>
<td width='13%'>&nb sp;Peter</td>
<td width='12%'><in put type=text name=date[] size=8></td>
.......
......

May 18 '07 #1
8 1994
On May 18, 7:02 am, chima...@google mail.com wrote:
Hi Everyone,
I have a huge problem. I am trying to get two corresponding variables
from a Checkbox and a Text box. e.g. If I check a value, say:
enrolled? [] (checkbox) then I have to put the date enrolled as a
text.
I'm kind of confused- you mean the form is submitted, and if the
enrolled checkbox was checked, populate the enrollment date text
field? I'll go on that assumption.
So when inserting in a table I want the enrolled to correspond
to each date. Here is what I am trying but not working (In the echos
is where I am trying to have the values into the table):

while (list($key, $val) = @each ($box))
{
echo "$val";
while (list($key, $v_val) = @each ($dose))
{
echo "$v_val";
}}

echo "<form method=post action=''>";
echo "<table border='0' cellspacing='0' style='border-collapse:
collapse' width='100' >

<tr bgcolor='white' >
<td width='25%'><in put type=checkbox name=box[] value='John'></td>
<td width='13%'>&nb sp;John</td>
<td width='12%'><in put type=text name=date[] size=8></td>
<td width='25%'><in put type=checkbox name=box[] value='Peter'></td>
<td width='13%'>&nb sp;Peter</td>
<td width='12%'><in put type=text name=date[] size=8></td>
......
.....
[?php
if($enrolled == "on") {
$enrollment_dat e = $dbh->query("SELEC T enroll_date FROM students
WHERE id = 'studentid'");
}
?]
[input type='text' name='enrollmen t_date' value='[?=
$enrollment_dat e; ?]']

Like that? Am I missing something?

May 18 '07 #2
On 18 May, 15:10, "rickycorn...@g mail.com" <rickycorn...@g mail.com>
wrote:
On May 18, 7:02 am, chima...@google mail.com wrote:
Hi Everyone,
I have a huge problem. I am trying to get two corresponding variables
from a Checkbox and a Text box. e.g. If I check a value, say:
enrolled? [] (checkbox) then I have to put the date enrolled as a
text.

I'm kind of confused- you mean the form is submitted, and if the
enrolled checkbox was checked, populate the enrollment date text
field? I'll go on that assumption.


So when inserting in a table I want the enrolled to correspond
to each date. Here is what I am trying but not working (In the echos
is where I am trying to have the values into the table):
while (list($key, $val) = @each ($box))
{
echo "$val";
while (list($key, $v_val) = @each ($dose))
{
echo "$v_val";
}}
echo "<form method=post action=''>";
echo "<table border='0' cellspacing='0' style='border-collapse:
collapse' width='100' >
<tr bgcolor='white' >
<td width='25%'><in put type=checkbox name=box[] value='John'></td>
<td width='13%'>&nb sp;John</td>
<td width='12%'><in put type=text name=date[] size=8></td>
<td width='25%'><in put type=checkbox name=box[] value='Peter'></td>
<td width='13%'>&nb sp;Peter</td>
<td width='12%'><in put type=text name=date[] size=8></td>
......
.....

[?php
if($enrolled == "on") {
$enrollment_dat e = $dbh->query("SELEC T enroll_date FROM students
WHERE id = 'studentid'");}

?]
[input type='text' name='enrollmen t_date' value='[?=
$enrollment_dat e; ?]']

Like that? Am I missing something?- Hide quoted text -

- Show quoted text -
Thanks for the reply but what I want is like this (hypothetically ):

Student name [Peter] Enrolled? [] Date Enrolled [ ]
Student name [John ] Enrolled? [] Date Enrolled [ ]

Then applying the loop to insert the two records. Hope this makes
sense..

May 18 '07 #3
I am not exactly following your logic.

So if you check the Enrolled check box it should store in the database
the date that it was checked...
Do you want the date to appear automatically next to the check box
once it is checked?
If so maybe you should look into Javascript.
If you want to access database information without reloading the page
then use AJAX.

I hope I am not too far off.

May 18 '07 #4
Thanks for the reply but what I want is like this (hypothetically ):
>
Student name [Peter] Enrolled? [] Date Enrolled [ ]
Student name [John ] Enrolled? [] Date Enrolled [ ]
Iirc this works. didn't test it cause im lazy :-)

<input type="text" name="person[1][enrolled]">
<input type="text" name="person[1][date]">
<input type="text" name="person[2][enrolled]">
<input type="text" name="person[2][date]">

foreach ($_POST['person'] as $id=>$person)
{
$sql="UPDATE $db SET enrolled='".$pe rson['enrolled']."',
date='".$person['date']."' WHERE id = '$person'";
// etc
}

--
Arjen
www.arjenkarel.nl -- my crappy excuse for a website (just gathering dust)
May 18 '07 #5
Floortje wrote:

Im an idiot :-)
$sql="UPDATE $db SET enrolled='".$pe rson['enrolled']."',
date='".$person['date']."' WHERE id = '$id'";
--
Arjen
www.arjenkarel.nl -- my crappy excuse for a website (just gathering dust)
May 18 '07 #6
On 18 May, 20:16, Floortje <floor...@dont. mailwrote:
Floortje wrote:

Im an idiot :-)
$sql="UPDATE $db SET enrolled='".$pe rson['enrolled']."',
date='".$person['date']."' WHERE id = '$id'";

--
Arjenwww.arjenk arel.nl-- my crappy excuse for a website (just gathering dust)
Still not inserting the values. Maybe let me get an exact extract from
my code:
<?php require_once('./Connections/irdb.php'); ?>
<?php
$hos_no = $_GET["h_no"];

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_zz = 10;
$pageNum_zz = 0;
if (isset($_GET['pageNum_zz']))
{
$pageNum_zz = $_GET['pageNum_zz'];
}
$startRow_zz = $pageNum_zz * $maxRows_zz;

mysql_select_db ($database_irdb , $irdb);
$query_sl = "SELECT fname, sname FROM irdb_patients WHERE
irdb_patients.h ospital_number ='$hos_no'";
$sl = mysql_query($qu ery_sl, $irdb) or die(mysql_error ());
$row_sl = mysql_fetch_ass oc($sl);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>Medicati ons</title>

</head>
<body>
<table align='left'>
<font face="Verdana" size="5"><b>Oth er Medications
</b></font>
</table>

<br>
<br>
<br>
<table border="0" width="100%" cellspacing="1" >
<tr>
<td width="594"><b> <font face="Verdana"> Patient Name:&nbsp;<?ph p
echo $row_sl['fname'] ?>&nbsp;&nbsp;& nbsp;&nbsp;<?ph p echo
$row_sl['sname'] ?>
</font </b </td>
</tr>
</table>
<?php
$today = date('Y-m-d');
echo "Date: ";
echo date('d-m-Y', strtotime($toda y));
while (list($key, $val) = @each ($box))
{
echo "$val";
while (list($key, $v_val) = @each ($dose))
{

echo "$v_val";
}
}

$mysql_query="i nsert into x_tbl (a, b, c) "."values ('$v_val, $v_val,
$today) ";
mysql_query($my sql_query) or die ('Error Insertion into Other Co
Morbidities Failed');
}
echo "<form method=post action=''>";
echo "<table border='0' cellspacing='0' style='border-collapse:
collapse' width='100' >

<tr bgcolor='white' >
<td width='25%'><in put type=checkbox name=box[] value='Adalimam ub'></
td>
<td width='13%'>&nb sp;Adalimamub</td>
<td width='12%'><in put type=text name=dose[] size=8></td>
<td width='25%'><in put type=checkbox name=box[] value='Azathiop rine'></
td>
<td width='13%'>&nb sp;Azathioprine </td>
<td width='12%'><in put type=text name=dose[] size=8></td>
<td width='25%'><in put type=checkbox name=box[]
value='Bendrofl uazide'></td>
<td width='13%'>&nb sp;Bendrofluazi de</td>
<td width='12%'><in put type=text name=dose[] size=8></td>
<td width='25%'><in put type=checkbox name=box[] value='Calcium' ></td>
<td width='13%'>&nb sp;Calcium</td>
<td width='12%'><in put type=text name=dose[] size=8></td>

</table>";

?><body bgcolor="white" >

<b><font face="Verdana" size="2" color="#FF0000" >**Please ensure
that the medications are correct before saving otherwise go back to
previous page.</font></b>

</body>

</html>

May 18 '07 #7
ch******@google mail.com schreef:
On 18 May, 20:16, Floortje <floor...@dont. mailwrote:
>Floortje wrote:

Im an idiot :-)
>>$sql="UPDAT E $db SET enrolled='".$pe rson['enrolled']."',
date='".$pers on['date']."' WHERE id = '$id'";
--
Arjenwww.arjen karel.nl-- my crappy excuse for a website (just gathering dust)

Still not inserting the values. Maybe let me get an exact extract from
my code:
The code shoudl work after a liitle modification.

Try var_dump($_POST ) and see what you get

--
Arjen
http://www.hondenpage.com
May 19 '07 #8
On 19 May, 10:21, Floortje <l...@zingmaarm etmijmee.enelwr ote:
chima...@google mail.com schreef:On 18 May, 20:16, Floortje <floor...@dont. mailwrote:
Floortje wrote:
Im an idiot :-)
>$sql="UPDATE $db SET enrolled='".$pe rson['enrolled']."',
date='".$perso n['date']."' WHERE id = '$id'";
--
Arjenwww.arjenk arel.nl--my crappy excuse for a website (just gathering dust)
Still not inserting the values. Maybe let me get an exact extract from
my code:

The code shoudl work after a liitle modification.

Try var_dump($_POST ) and see what you get

--
Arjenhttp://www.hondenpage. com

I have tried but to no avail. I am able to get the values of the text
box. But what I am not sure is how to handle the checkbox values. You
suggested:

<input type="text" name="person[1][enrolled]">
<input type="text" name="person[1][date]">
<input type="text" name="person[2][enrolled]">
<input type="text" name="person[2][date]">

But what I actually need is:

<input type=checkbox name=box[] value="X"-- Supposed to insert "X"
if checked
<input type="text" name="person[1][date]"-- Deals with variable
dates per corresponding checkbox
<input type=checkbox name=box[] value="Y"-- Supposed to insert "Y"
if checked
<input type="text" name="person[2][date]"-- Deals with variable
dates per corresponding checkbox
May 24 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
2384
by: Thomas Mlynarczyk | last post by:
Hello, My provider has set register_globals = On and I can't change the php.ini file. Is there a way to unset all the imported get/post etc. variables at the beginning of my script? Thomas
6
4793
by: Jay donnell | last post by:
I have a short multi-threaded script that checks web images to make sure they are still there. I get a segmentation fault everytime I run it and I can't figure out why. Writing threaded scripts is new to me so I may be doing something wrong that should be obvious :( google messes up the python code so here is a link to it. http://kracomp.com/~jay/py.txt This is the output of the script.
8
2559
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
5
14808
by: David Rasmussen | last post by:
If I have a string that contains the name of a function, can I call it? As in: def someFunction(): print "Hello" s = "someFunction" s() # I know this is wrong, but you get the idea... /David
5
2419
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at the end of the iteration. Kindly note that I am not talking about dynamically allocating memory within a loop, which is a perfectly valid operation (albeit with a chance of resulting in a memory leak, unless we are careful). The most common...
9
2490
by: Javaman59 | last post by:
Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } }
9
3969
by: Good Man | last post by:
Hi This is sort of a weird question, perhaps a bit off-topic... I am on the 'edit' screen of a web form, and I have a bunch of variables coming from a database that need to be placed into the form. In the past, I have been using PHP to pre-populate each field, something like <input type="text" id="firstName" value="<?= $first_name ?>" />
41
2887
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be created and then added to "component" objects to build up the component definition. My dilemma comes in deciding how to read/write data to the "item"
7
4446
vikas251074
by: vikas251074 | last post by:
I am getting error above in following code since few days giving tension day and night. How can I solve this? I am facing since Oct.25. in line no. 362 After doing a lot of homework, I am surrendered to you. <%@ Language=VBScript%> <%Option Explicit%>
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.