php code in a form / on click of radio button
Hello i have a page called Buy.html the main focus on the page is the form. My website allows people to buy textbooks from one another. I have a database that keeps track of all the textbooks.
Here is the main code for the page:
Code:
<div align="center">
<table border="0" width="650" bgcolor="#FFFFFF">
<tr>
<td> </td>
<td>
<!--Page below-->
<p align="center"><font size=5><b>Buy</b></font></p>
<br />
<br />
<div>
<form>
<b>Province</b>: <input type="text"></input>
<br />
<b>City</b>: <input type="text"></input>
<br />
<b>School</b>: <input type="text"></input> <b>Campus</b>: <input type="text"></input>
<br />
<b>Grade</b>: <input type="text"></input> <br /><br />
<input type="radio" Name= "ByGrade_Radio" /> Pick all text books for Grade. <br />
<input type="radio" Name= "ByCourse_Radio" /> Pick all text books by Course.
</form>
</div>
<!--Page Ends-->
<p> </td>
<td> </td>
</tr>
</table>
</div>
I want it so when the user gets to the two radio buttons and choses between them some php code will execute. For example i want it so when the user selects the ByGrade_Radio radio button checkboxes will start to be listed for each textbook found in the database with the matching grade.
As well since the php will be creating these checkboxes i need to be able to retrieve whether a checkbox is checked or not using $_POST, so i may process the input since it is a form.
The major thing i am not sure about is how i will retrieve which checkbox is checked since the checkboxes are being generated from the mySQL database.
Is this possible? If not, does anyone have other suggestions/alternates...
Re: php code in a form / on click of radio button
the only way to do this dynamically is with javascript -- or more specifically, ajax. however, if you have very little knowledge with PHP then I don't think it would be worth getting into that. if you're still interested, you can look up ajax tutorials online, there are plenty which should let you get a grasp of the concept.
without using ajax, you could simply get the user to press an "update" button after they've selected a one of the radio buttons, or automatically submit the form with javascript to emulate this. for the javascript method, something like this should work:
Code:
<form name="myform" action="thispage.php" method="post">
<input name="radiobutton" value="1" type="radio" onclick="document.myform.submit();" /> radio button option 1<br />
<input name="radiobutton" value="2" type="radio" onclick="document.myform.submit();" /> radio button option 2
</form>
then, you can use simple PHP to look at $_POST['radiobutton'] and decide what information to get from the database depending on the value.
also, from the looks of your form, you're using radio buttons incorrectly. radio buttons are a set of options, of which can only have one value. they are all named the same, but have different values. if I didn't know any better, I would think you meant to have your radio buttons set up like this:
Code:
<input type="radio" name="myradiobutton" value="ByGrade_Radio" /> Pick all text books for Grade. <br />
<input type="radio" name="myradiobutton" value="ByCourse_Radio" /> Pick all text books by Course.
as far as recognizing checkboxes when they're being submitted to a form, the checkbox name will not exist in the POST array unless the checkbox was checked. if any of the checkboxes are left unchecked, they will simply "not exist" when the form is submitted. I don't know why you aren't sure about this part. when using ajax, or when using the alternate method I described above, you're simply adding to the HTML of the form; when you finally submit the form, the values of whatever checkboxes were checked will be sent just as if those checkboxes were in the original form.
hope that makes sense. ask questions.
Re: php code in a form / on click of radio button
Thank you, i will take another look at it, also i just remembered i have a program which i havn't used in a while but will generate most of the code for that part...