By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

Getting two variables from a PHP loop

P: n/a
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%'><input type=checkbox name=box[] value='John'></td>
<td width='13%'>&nbsp;John</td>
<td width='12%'><input type=text name=date[] size=8></td>
<td width='25%'><input type=checkbox name=box[] value='Peter'></td>
<td width='13%'>&nbsp;Peter</td>
<td width='12%'><input type=text name=date[] size=8></td>
.......
......

May 18 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On May 18, 7:02 am, chima...@googlemail.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%'><input type=checkbox name=box[] value='John'></td>
<td width='13%'>&nbsp;John</td>
<td width='12%'><input type=text name=date[] size=8></td>
<td width='25%'><input type=checkbox name=box[] value='Peter'></td>
<td width='13%'>&nbsp;Peter</td>
<td width='12%'><input type=text name=date[] size=8></td>
......
.....
[?php
if($enrolled == "on") {
$enrollment_date = $dbh->query("SELECT enroll_date FROM students
WHERE id = 'studentid'");
}
?]
[input type='text' name='enrollment_date' value='[?=
$enrollment_date; ?]']

Like that? Am I missing something?

May 18 '07 #2

P: n/a
On 18 May, 15:10, "rickycorn...@gmail.com" <rickycorn...@gmail.com>
wrote:
On May 18, 7:02 am, chima...@googlemail.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%'><input type=checkbox name=box[] value='John'></td>
<td width='13%'>&nbsp;John</td>
<td width='12%'><input type=text name=date[] size=8></td>
<td width='25%'><input type=checkbox name=box[] value='Peter'></td>
<td width='13%'>&nbsp;Peter</td>
<td width='12%'><input type=text name=date[] size=8></td>
......
.....

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

?]
[input type='text' name='enrollment_date' value='[?=
$enrollment_date; ?]']

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

P: n/a
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

P: n/a
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='".$person['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

P: n/a
Floortje wrote:

Im an idiot :-)
$sql="UPDATE $db SET enrolled='".$person['enrolled']."',
date='".$person['date']."' WHERE id = '$id'";
--
Arjen
www.arjenkarel.nl -- my crappy excuse for a website (just gathering dust)
May 18 '07 #6

P: n/a
On 18 May, 20:16, Floortje <floor...@dont.mailwrote:
Floortje wrote:

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

--
Arjenwww.arjenkarel.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.hospital_number ='$hos_no'";
$sl = mysql_query($query_sl, $irdb) or die(mysql_error());
$row_sl = mysql_fetch_assoc($sl);

?>

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

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

</head>
<body>
<table align='left'>
<font face="Verdana" size="5"><b>Other 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;<?php
echo $row_sl['fname'] ?>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo
$row_sl['sname'] ?>
</font </b </td>
</tr>
</table>
<?php
$today = date('Y-m-d');
echo "Date: ";
echo date('d-m-Y', strtotime($today));
while (list($key, $val) = @each ($box))
{
echo "$val";
while (list($key, $v_val) = @each ($dose))
{

echo "$v_val";
}
}

$mysql_query="insert into x_tbl (a, b, c) "."values ('$v_val, $v_val,
$today) ";
mysql_query($mysql_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%'><input type=checkbox name=box[] value='Adalimamub'></
td>
<td width='13%'>&nbsp;Adalimamub</td>
<td width='12%'><input type=text name=dose[] size=8></td>
<td width='25%'><input type=checkbox name=box[] value='Azathioprine'></
td>
<td width='13%'>&nbsp;Azathioprine</td>
<td width='12%'><input type=text name=dose[] size=8></td>
<td width='25%'><input type=checkbox name=box[]
value='Bendrofluazide'></td>
<td width='13%'>&nbsp;Bendrofluazide</td>
<td width='12%'><input type=text name=dose[] size=8></td>
<td width='25%'><input type=checkbox name=box[] value='Calcium'></td>
<td width='13%'>&nbsp;Calcium</td>
<td width='12%'><input 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

P: n/a
ch******@googlemail.com schreef:
On 18 May, 20:16, Floortje <floor...@dont.mailwrote:
>Floortje wrote:

Im an idiot :-)
>>$sql="UPDATE $db SET enrolled='".$person['enrolled']."',
date='".$person['date']."' WHERE id = '$id'";
--
Arjenwww.arjenkarel.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

P: n/a
On 19 May, 10:21, Floortje <l...@zingmaarmetmijmee.enelwrote:
chima...@googlemail.com schreef:On 18 May, 20:16, Floortje <floor...@dont.mailwrote:
Floortje wrote:
Im an idiot :-)
>$sql="UPDATE $db SET enrolled='".$person['enrolled']."',
date='".$person['date']."' WHERE id = '$id'";
--
Arjenwww.arjenkarel.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 discussion thread is closed

Replies have been disabled for this discussion.