473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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%'><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
8 1977
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
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
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='".$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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
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
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...
8
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...
5
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... ...
5
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...
9
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
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...
41
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...
7
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.