Jump to content

PHP and Javascript form issue

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
Hello everyone,

This is my first post in a forum, so I apologize if I have made some grave error. I am relatively new to php and know very little about javascript, so I have hit a roadblock in my endeavor. I have read a bunch of data from a mysql database, and I display it on the screen. Then I want to be able to change fields within the database based on what checkboxes are ticked and text is added. That may sound confusing, so I have included my code on the bottom of this post. I do understand enough about php to know that it is running server side, and that all my php requests are done pre-user interaction. That is why I have been unable to come up with a solution to my problem.

If anyone could give me insight onto a better method of achieving my goal, I would greatly appreciate the help.

Thank you for taking the time to read my post, and I hope to hear from some of the intelligent minds out there.

SirBee

<?php


include 'connect.PHP';


$registrationQuery=mysql_query("SELECT

`registration`.`registrationID` AS `registrationID`,

`registration`.`semi_identifier` AS `semi_identifier`,

`registration`.`teamName` AS `teamName`,

`registration`.`userName` AS `userName`,

`registration`.`cFirst` AS `cFirst`,

`registration`.`cLast` AS `cLast`,

`registration`.`comments` AS `comments`,

`registration`.`registered` AS `registered`,

`registration`.`paid` AS `paid`,

`registration`.`notes` AS `notes`

FROM

`regcategory`

Inner Join `registration` ON `regcategory`.`semi_identifier` = `registration`.`semi_identifier`

WHERE

`regcategory`.`season` =  'summer' ORDER BY `regcategory`.`semi_identifier`, `registration`.`registrationID`");


?>

<TABLE ALIGN=CENTER cellspacing='2' CELLPADDING='2'>

<TR><TD ALIGN=CENTER colspan=6><font size=4 face=arial><BR><b>Registrations - Sorted by league</b><BR><BR></font></TD></TR>

<?php

$teamTemp="";

while($registration_array=mysql_fetch_array($registrationQuery)){


	$team=$registration_array['semi_identifier'];

	if($team!=$teamTemp){

		$teamNum=0;

		$teamTemp=$team;

		$q=mysql_query("SELECT * FROM regcategory WHERE semi_identifier='$team'");

		$a=mysql_fetch_array($q);	 

		?>

		<TR><TD align=center colspan=7 bgcolor="#33CCFF"><font size=2><b><?php print $a['category']?></b></font></TD>

		<TD align="center"><input type="submit" value="Save Changes"></TD></TR>

		<TR><TD align=center><font size=2><b><u>Team Number</TD><TD align=center><font size=2><b><u>Team Name</TD>

		<TD align=center><font size=2><b><u>Captain</TD><TD align=center><font size=2><b><u>Username</TD><TD align=center><font size=2><b><u>Con (Y/N)</TD>

		<TD align=center><font size=2><b><u>Paid</TD><TD align=center><font size=2><b><u>Delete</TD><TD align=center><font size=2><b><u>Notes</TD></TR>

		<?php

	}

	$teamNum+=1;

	?>    

	<TR><TD align=center><font size=2><?php print $teamNum;?></td><TD align=center><font size=2><?php print $registration_array['teamName'];?></td>

	<TD align=center><font size=2><?php print $registration_array['cFirst']." ".$registration_array['cLast'];?></td>

	<TD align=center><font size=2><?php print $registration_array['userName']?></td>

	<TD align=center><font size=2><?php if($registration_array['registered']=='1'){print "<B>Y</B>";}else{print "N";}?></td>


	<TD align=center>

	<?php if($registration_array['paid']=='1'){?><input type="checkbox" checked name="paid" value="1"></TD><?php }else{?><input type="checkbox" name="paid" value="1"></TD>

	<?php }?> <TD align=center><input type="checkbox" name="delete" value="1"></TD><TD><input type="text" name="notes"></TD>

	<?php if($registration_array['comments']!=""){?><TR><TD></TD><TD COLSPAN=6 align=LEFT width=500><FONT SIZE=2 color='blue'><i>

	<?php print 'Comment: "'.nl2br($registration_array['comments']).'"';?></TD></TR<?php }?>

	<?php if($registration_array['notes']!=""){?><TR><TD></TD><TD COLSPAN=6 align=LEFT width=500><FONT SIZE=2 color='red'><i>

	<?php print 'Notes: "'.nl2br($registration_array['notes']).'"';?></TD></TR<?php }

}?>

</TABLE>



#2
jimpossible2k

jimpossible2k

    Newbie

  • Members
  • Pip
  • 5 posts
Basically you have to wrap your table in the <form></form> tags and have the form action = "myFile.php" (where myFile is the name of the file you have given), ie: a php file with a form that submits to itself. Include a hidden input field somewhere in the form to indicate whether the form has been submitted: <input type="hidden" name="wasSubmitted" value="1" />. Then for each field in the database you want to modify, include a form input element of the appropriate type, and have the value default to whatever is in the database: for example, <input type="text" name="teamName" value="<?php echo $registration_array['teamName'] ?>" />.

Lastly, when the submit button is pressed, at the top of your file (in the PHP) you would have an if-condition: if($_REQUEST['wasSubmitted']) { ... }. Inside this if-statement you would take out all the information from the form-submittion (all stored in the $_REQUEST variable) and do the appropriate sql insert query.
Posted Image

#3
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
Thank you for the reply. I will add that in and let you know how it turns out.

Thanks again,

SirBee

#4
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
I appreciate your help jimpossible2k, your input pushed me past one mental block, and I was able to advance from there. However, as always it seems, with progress comes more road blocks. I have used the form method, but I left the action blank, so it would reload the initial page every time the submit button was hit. I have also added a drop down menu to restrict the table to display only one league at a time. My next problem comes from the $_POST variable. I have it set to return a registration id for each paid button that is checked, but I do not know how to access all the entries. the echo at the bottom of my program will only print out the final paid entry, and I do not know how to get to any of the others. Your help is greatly appreciated.

Thank you,

SirBee

<?php

include 'connect.PHP';


?>

<form action="" method="post">
<div align="center">
<select name="league">
<option value="001">Intermediate 6's Sunday</option>
<option value="002">Recreational 6's Sunday</option>
<option value="011">Open 2's I Monday</option>
<option value="012">Open 2's II Monday</option>
<option value="003">Competitve 4's Monday</option>
<option value="004">Intermediate 4's Monday</option>
<option value="005">Competitve 4's Tuesday</option>
<option value="006">Intermediate 4's Tuesday</option>
<option value="007">Intermediate 6's Wednesday</option>
<option value="008">Recreational 6's Wednesday</option>
<option value="009">Intermediate 6's Thursday</option>
<option value="010">Recreational 6's Thursday</option>
<option value="101">A1 Monday</option>
<option value="102">A2 Thursday</option>
<option value="103">B1 Wednesday</option>
<option value="104">B2 Wednesday</option>
<option value="105">C1/C2 Tuesday</option>
<option value="109">C1 Thursday</option>
<option value="110">C2 Thursday</option>
<option value="202">B1/B2 Tuesday</option>
<option value="301">Recreational Monday</option>
<option value="302">Recreational Tuesday</option>
<option value="303">Recreational Wednesday</option>
<option value="304">Recreational Thursday</option>
<option value="000">All</option>
</select>
<input type="submit" name="Submit">
<input type="hidden" name="leagueSubmitted" value="1">
</div>
</form>

<?php $leagueUse=$_POST['league']; 

if($leagueUse!='000'){
$registrationQuery=mysql_query("SELECT
`registration`.`registrationID` AS `registrationID`,
`registration`.`semi_identifier` AS `semi_identifier`,
`registration`.`teamName` AS `teamName`,
`registration`.`userName` AS `userName`,
`registration`.`cFirst` AS `cFirst`,
`registration`.`cLast` AS `cLast`,
`registration`.`comments` AS `comments`,
`registration`.`registered` AS `registered`,
`registration`.`paid` AS `paid`,
`registration`.`notes` AS `notes`
FROM
`regcategory`
Inner Join `registration` ON `regcategory`.`semi_identifier` = `registration`.`semi_identifier`
WHERE
`regcategory`.`cat_id` =  $leagueUse AND `regcategory`.`season` = 'summer' ORDER BY `regcategory`.`semi_identifier`, `registration`.`registrationID`");
}else if($leagueUse=='000'){
$registrationQuery=mysql_query("SELECT
`registration`.`registrationID` AS `registrationID`,
`registration`.`semi_identifier` AS `semi_identifier`,
`registration`.`teamName` AS `teamName`,
`registration`.`userName` AS `userName`,
`registration`.`cFirst` AS `cFirst`,
`registration`.`cLast` AS `cLast`,
`registration`.`comments` AS `comments`,
`registration`.`registered` AS `registered`,
`registration`.`paid` AS `paid`,
`registration`.`notes` AS `notes`
FROM
`regcategory`
Inner Join `registration` ON `regcategory`.`semi_identifier` = `registration`.`semi_identifier`
WHERE
`regcategory`.`season` = 'summer' ORDER BY `regcategory`.`semi_identifier`, `registration`.`registrationID`");
}

?>
<TABLE ALIGN=CENTER cellspacing='2' CELLPADDING='2'>
<TR><TD ALIGN=CENTER colspan=6><font size=4 face=arial><BR><b>Registrations - Sorted by league</b><BR><BR></font></TD></TR>
<?php
$teamTemp="";
?><form action="" method="post"><?php
while($registration_array=mysql_fetch_array($registrationQuery)){

	
	$team=$registration_array['semi_identifier'];
	if($team!=$teamTemp){
		$teamNum=0;
		$teamTemp=$team;
		if ($leagueUse!='000'){		
			$q=mysql_query("SELECT * FROM regcategory WHERE cat_id='$leagueUse'");
		}else if ($leagueUse=='000'){
			$q=mysql_query("SELECT * FROM regcategory WHERE semi_identifier='$team'");
		}
		$a=mysql_fetch_array($q);	 
		?>
		<TR><TD align=center colspan=7 bgcolor="#33CCFF"><font size=2><b><?php print $a['category'];?></b></font></TD>
		<TD align="center"><input type="submit" value="Save Changes"></TD></TR>
		<TR><TD align=center><font size=2><b><u>Team Number</TD><TD align=center><font size=2><b><u>Team Name</TD>
		<TD align=center><font size=2><b><u>Captain</TD><TD align=center><font size=2><b><u>Username</TD><TD align=center><font size=2><b><u>Con (Y/N)</TD>
		<TD align=center><font size=2><b><u>Paid</TD><TD align=center><font size=2><b><u>Delete</TD><TD align=center><font size=2><b><u>Notes</TD></TR>
		<?php
	}
	$teamNum+=1;
	?>    
	<TR><TD align=center><font size=2><?php print $teamNum?></td><TD align=center><font size=2><?php print $registration_array['teamName'];?></td>
	<TD align=center><font size=2><?php print $registration_array['cFirst']." ".$registration_array['cLast'];?></td>
	<TD align=center><font size=2><?php print $registration_array['userName']?></td>
	<TD align=center><font size=2><?php if($registration_array['registered']=='1'){print "<B>Y</B>";}else{print "N";}?></td>

	<TD align=center>
	<?php if($registration_array['paid']=='1'){?><input type="checkbox" checked name="paid[]" value="<?php echo $registration_array['registrationID'] ?>"></TD><?php }else{?><input type="checkbox" name="paid[]" value="<?php echo $registration_array['registrationID'] ?>"></TD>
	<?php }?> <TD align=center><input type="checkbox" name="delete" value="<?php echo $registration_array['registrationID'] ?>"></TD><TD><input type="text" name="notes"></TD>
	<?php if($registration_array['comments']!=""){?><TR><TD></TD><TD COLSPAN=6 align=LEFT width=500><FONT SIZE=2 color='blue'><i>
	<?php print 'Comment: "'.nl2br($registration_array['comments']).'"';?></TD></TR<?php }?>
	<?php if($registration_array['notes']!=""){?><TR><TD></TD><TD COLSPAN=6 align=LEFT width=500><FONT SIZE=2 color='red'><i>
	<?php print 'Notes: "'.nl2br($registration_array['notes']).'"';?></TD></TR><input type="hidden" name="saveChanges" value="1"/><?php }
}?></form>
</TABLE>

<?php

echo $_POST['paid'];

?>


#5
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
I am working through this slowly. I have added to one of the lines to distinguish different paid boxes, but I still cannot access them. I have changed it to GET just so I can see what variables I have, and it did change to paid[1], but when I try to echo $_GET['paid[1]'], I get nothing.

<?php if($registration_array['paid']=='1'){?><input type="checkbox" checked name="paid[<?php echo $teamNum?>]" value="<?php echo $registration_array['registrationID'] ?>"></TD><?php }else{?><input type="checkbox" name="paid[<?php echo $teamNum?>]" value="<?php echo $registration_array['registrationID'] ?>"></TD>


#6
geget

geget

    Newbie

  • Members
  • Pip
  • 7 posts
Have you seen a value of 'paid[1]' by means of URL?
Please show the fragment of code with echo $_GET['paid[1]'] completely.

#7
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
Yes, I see a value for 'paid[1]' in the URL. If I were to tick multiple boxes I would see values for 'paid[2]' or whichever other boxes I ticked. The following is the code fragment you were asking for. Using that code I expected one of them to work, but I get no display.

<?php

$paid=$_GET['paid[1]'];
print $_GET['paid[1]'];
print $paid;
echo $_GET['paid[1]'];
echo $paid;

?>


#8
geget

geget

    Newbie

  • Members
  • Pip
  • 7 posts
I don't know the reason, but it's discounting a '[]' sign. May be it will be beter to write without '[]', like this
<?php if($registration_array['paid']=='1'){?><input type="checkbox" checked name="paid<?php echo $teamNum?>" value="<?php echo $registration_array['registrationID']

#9
geget

geget

    Newbie

  • Members
  • Pip
  • 7 posts
Also you can try
$arr=$_GET['paid'];
echo $arr[1];

#10
geget

geget

    Newbie

  • Members
  • Pip
  • 7 posts
paid[1] interpreted as part of array in URL :)

#11
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
Your last suggestion worked perfectly. Thank you very much geget.

#12
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
I am not sure if this is related to the title anymore, seeing as there is no Javascript involved, but I have encountered another php/html for problem. At the bottom of my code, I have created a for loop to handle my array of form entries. I then use that for loop to update mySQL database. The problem I have encountered is that every time the submit button is hit, my $teamCount and $teamNum are reset to zero. I need a method to save that number so I can use it in the for loop, but I also need it to reset so the counter is not off the next time the page runs. I was thinking using an if ($_POST['saveChanges']){$count=$teamCount;}, but that makes no difference as saveChanges never seams to be 1. I see that it never seems to be 1 from the commented out output near the bottom of the code.

Thank you in advance for your help.

SirBee.

<?php

include 'connect.PHP';


?>

<form action="" method="post">
<div align="center">
<select name="league">
<option value="001">Intermediate 6's Sunday</option>
<option value="002">Recreational 6's Sunday</option>
<option value="011">Open 2's I Monday</option>
<option value="012">Open 2's II Monday</option>
<option value="003">Competitve 4's Monday</option>
<option value="004">Intermediate 4's Monday</option>
<option value="005">Competitve 4's Tuesday</option>
<option value="006">Intermediate 4's Tuesday</option>
<option value="007">Intermediate 6's Wednesday</option>
<option value="008">Recreational 6's Wednesday</option>
<option value="009">Intermediate 6's Thursday</option>
<option value="010">Recreational 6's Thursday</option>
<option value="101">A1 Monday</option>
<option value="102">A2 Thursday</option>
<option value="103">B1 Wednesday</option>
<option value="104">B2 Wednesday</option>
<option value="105">C1/C2 Tuesday</option>
<option value="109">C1 Thursday</option>
<option value="110">C2 Thursday</option>
<option value="202">B1/B2 Tuesday</option>
<option value="301">Recreational Monday</option>
<option value="302">Recreational Tuesday</option>
<option value="303">Recreational Wednesday</option>
<option value="304">Recreational Thursday</option>
<option value="000">All</option>
</select>
<input type="submit" name="Submit">
<input type="hidden" name="leagueSubmitted" value="1">
</div>
</form>

<?php $leagueUse=$_POST['league']; 

if($leagueUse!='000'){
$registrationQuery=mysql_query("SELECT
`registration`.`registrationID` AS `registrationID`,
`registration`.`semi_identifier` AS `semi_identifier`,
`registration`.`teamName` AS `teamName`,
`registration`.`userName` AS `userName`,
`registration`.`cFirst` AS `cFirst`,
`registration`.`cLast` AS `cLast`,
`registration`.`comments` AS `comments`,
`registration`.`registered` AS `registered`,
`registration`.`paid` AS `paid`,
`registration`.`notes` AS `notes`
FROM
`regcategory`
Inner Join `registration` ON `regcategory`.`semi_identifier` = `registration`.`semi_identifier`
WHERE
`regcategory`.`cat_id` =  $leagueUse AND `regcategory`.`season` = 'summer' ORDER BY `regcategory`.`semi_identifier`, `registration`.`registrationID`");
}else if($leagueUse=='000'){
$registrationQuery=mysql_query("SELECT
`registration`.`registrationID` AS `registrationID`,
`registration`.`semi_identifier` AS `semi_identifier`,
`registration`.`teamName` AS `teamName`,
`registration`.`userName` AS `userName`,
`registration`.`cFirst` AS `cFirst`,
`registration`.`cLast` AS `cLast`,
`registration`.`comments` AS `comments`,
`registration`.`registered` AS `registered`,
`registration`.`paid` AS `paid`,
`registration`.`notes` AS `notes`
FROM
`regcategory`
Inner Join `registration` ON `regcategory`.`semi_identifier` = `registration`.`semi_identifier`
WHERE
`regcategory`.`season` = 'summer' ORDER BY `regcategory`.`semi_identifier`, `registration`.`registrationID`");
}

?>
<TABLE ALIGN=CENTER cellspacing='2' CELLPADDING='2'>
<TR><TD ALIGN=CENTER colspan=6><font size=4 face=arial><BR><b>Registrations - Sorted by league</b><BR><BR></font></TD></TR>
<?php
$teamTemp="";
?><form action="" method="post"><?php
$teamCount=0;
while($registration_array=mysql_fetch_array($registrationQuery)){

	$team=$registration_array['semi_identifier'];
	if($team!=$teamTemp){
		$teamNum=0;
		$teamTemp=$team;
		if ($leagueUse!='000'){		
			$q=mysql_query("SELECT * FROM regcategory WHERE cat_id='$leagueUse'");
		}else{
			$q=mysql_query("SELECT * FROM regcategory WHERE semi_identifier='$team'");
		}
		$a=mysql_fetch_array($q);	 
		?>
		<TR><TD align=center colspan=7 bgcolor="#33CCFF"><font size=2><b><?php print $a['category'];?></b></font></TD>
		<TD align="center"><input type="submit" value="Save Changes"></TD></TR>
		<TR><TD align=center><font size=2><b><u>Team Number</TD><TD align=center><font size=2><b><u>Team Name</TD>
		<TD align=center><font size=2><b><u>Captain</TD><TD align=center><font size=2><b><u>Username</TD><TD align=center><font size=2><b><u>Con (Y/N)</TD>
		<TD align=center><font size=2><b><u>Paid</TD><TD align=center><font size=2><b><u>Delete</TD><TD align=center><font size=2><b><u>Notes</TD></TR>
		<?php
	}
	$teamNum+=1;
	$teamCount+=1;
	?>    
	<TR><TD align=center><font size=2><?php print $teamNum?></td><TD align=center><font size=2><?php print $registration_array['teamName'];?></td>
	<TD align=center><font size=2><?php print $registration_array['cFirst']." ".$registration_array['cLast'];?></td>
	<TD align=center><font size=2><?php print $registration_array['userName']?></td>
	<TD align=center><font size=2><?php if($registration_array['registered']=='1'){print "<B>Y</B>";}else{print "N";}?></td>

	<TD align=center>
	<?php if($registration_array['paid']=='1'){?><input type="hidden" name="notpaid[<?php echo $teamCount?>]" value="<?php echo $registration_array['registrationID'] ?>"><input type="checkbox" checked name="paid[<?php echo $teamCount?>]" value="<?php echo $registration_array['registrationID'] ?>"></TD><?php }else{?><input type="hidden" name="notpaid[<?php echo $teamCount?>]" value="<?php echo $registration_array['registrationID'] ?>"><input type="checkbox" name="paid[<?php echo $teamCount?>]" value="<?php echo $registration_array['registrationID'] ?>"></TD>
	<?php }?> <TD align=center><input type="checkbox" name="delete[<?php echo $teamCount?>]" value="<?php echo $registration_array['registrationID'] ?>"></TD><TD><input type="text" name="notes"></TD>
	<?php if($registration_array['comments']!=""){?><TR><TD></TD><TD COLSPAN=6 align=LEFT width=500><FONT SIZE=2 color='blue'><i>
	<?php print 'Comment: "'.nl2br($registration_array['comments']).'"';?></TD></TR<?php }?>
	<?php if($registration_array['notes']!=""){?><TR><TD></TD><TD COLSPAN=6 align=LEFT width=500><FONT SIZE=2 color='red'><i>
	<?php print 'Notes: "'.nl2br($registration_array['notes']).'"';?></TD></TR><input type="hidden" name="saveChanges" value="1"/><?php }
}?></form>
</TABLE>

<?php

$paid=$_POST['paid'];
$notPaid=$_POST['notpaid'];
$delete=$_POST['delete'];

//echo "Team Num: ".$teamNum." Team Count: ".$teamCount." Save Changes: ".$_POST['saveChanges']." League Submitted: ".$_POST['leagueSubmitted'];

for ($i=1;$i<=$teamCount;$i++)
{
if ($paid[$i])
{
mysql_query("UPDATE registration SET paid='1' WHERE registrationID=$paid[$i]");
}
else
{
mysql_query("UPDATE registration SET paid='0' WHERE registrationID=$notPaid[$i]");
}
}

for ($l=1;$l<=$teamCount;$l++)
{
if ($paid[$l])
{
mysql_query("DELETE FROM registration WHERE registrationID=$paid[$l]");
}
}
?>