moonjazz Posted October 27, 2011 Share Posted October 27, 2011 Hi there. I have this course registering website whereby i will need to : 1. pull out all course info and display (done that) 2. when user choose the course they are interested in using radio button and click submit , it will display you're registered and update the database to decrease the head count by 1. i m stuck on the radio button part... after i click submit nothing happens... Please advice... thanks a lot <html> <head> <title>TimeTable</title> </head> <body> <h1> Here is the timetable</h1> <?php @ $db = new mysqli('localhost', 'root', '', 'mylifestyle'); if (mysqli_connect_errno()) { echo 'Error: Could not connect to database. Please try again later.'; exit; } $query = "SELECT course.CourseName , course.Instructor , course.ClassSize , courseschedule.Date ,courseschedule.Time , courseschedule.Location FROM course INNER JOIN courseschedule ON course.CourseID = courseschedule.CourseID"; $result = $db->query($query); $num_results = $result->num_rows; echo "<table border='1'> <tr> <th>Course Name</th> <th>Instructor</th> <th>Class size</th> <th>Date</th> <th>Time</th> <th>Location</th> <th>Register</th> </tr>"; ?> <?php for ($i=0; $i <$num_results; $i++) { $row = $result->fetch_assoc();?> <tr> <form action = "" method = "post"> <td><? echo $row['CourseName']; ?></td> <td><? echo $row['Instructor']; ?></td> <td><? echo $row['ClassSize']; ?></td> <td><? echo $row['Date']; ?></td> <td><? echo $row['Time']; ?></td> <td><? echo $row['Location']; ?></td> <td align="center"><input name="radio" type="radio" id="radio[]" value="<? echo $row['ClassSize']; ?>"></td> </tr> <?php } ?> <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="submit" type="submit" id="submit" value="submit"></td> <?php if($submit){ for($i=0;$i<$num_results;$i++){ $del_id = $radio[$i]; $query = "UPDATE course SET ClassSize = ClassSize -1 Where id='$del_id'"; $result = mysql_query($query); } // if successful redirect to delete_multiple.php if($result){ echo "$CourseName was deleted from database - myliftstyle!!<br />"; // Compute the item costs and total cost } } ?> </div> </body> </html> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/249933-passing-value-using-radio-button-and-update-the-database/ Share on other sites More sharing options...
Pikachu2000 Posted October 27, 2011 Share Posted October 27, 2011 if($submit) is meaningless if you don't assign the value of $_POST['submit'] to $submit. However, you really shouldn't depend on the submit button's value being present to see if a form has been submitted since some browsers mishandle that value. You'd be better off to either set a hidden field, and check for its value, or check to see if strtolower($_SERVER['REQUEST_METHOD']) === 'post' (as long as the form's method="post", that is). Quote Link to comment https://forums.phpfreaks.com/topic/249933-passing-value-using-radio-button-and-update-the-database/#findComment-1282795 Share on other sites More sharing options...
moonjazz Posted October 27, 2011 Author Share Posted October 27, 2011 Thanks for the respond... i have made some changes but it seems like the database is not updated.. am i doing it right? Please advice.. <html> <head> <title>TimeTable</title> </head> <body> <h1> Here is the timetable</h1> <?php @ $db = new mysqli('localhost', 'root', '', 'mylifestyle'); if (mysqli_connect_errno()) { echo 'Error: Could not connect to database. Please try again later.'; exit; } $query = "SELECT course.CourseName , course.Instructor , course.ClassSize , courseschedule.Date ,courseschedule.Time , courseschedule.Location FROM course INNER JOIN courseschedule ON course.CourseID = courseschedule.CourseID"; $result = $db->query($query); $num_results = $result->num_rows; echo "<table border='1'> <tr> <th>Course Name</th> <th>Instructor</th> <th>Class size</th> <th>Date</th> <th>Time</th> <th>Location</th> <th>Register</th> </tr>"; ?> <?php for ($i=0; $i <$num_results; $i++) { $row = $result->fetch_assoc();?> <form method="post" action="haha.php"> <tr> <td><? echo $row['CourseName']; ?></td> <td><? echo $row['Instructor']; ?></td> <td><? echo $row['ClassSize']; ?></td> <td><? echo $row['Date']; ?></td> <td><? echo $row['Time']; ?></td> <td><? echo $row['Location']; ?></td> <td align="center"><input name="radio" type="radio" id="radio[]" value="<? echo $row['ClassSize']; ?>"></td> </tr> <?php } ?> <p> <form method="post" action="<?php echo $PHP_SELF;?>"> <td> <input type = "submit" name="reg" value="Register" /></td> <input type = "hidden" name="formVar" value="<?php echo $ClassSize?>" /> </form> </p> </div> </body> </html> haha.php <?php if (!empty($_POST['reg'])) { $myvarA = $_POST['formVar']; $ClassSize=$myvarA; //echo "myvarA= "." $myvarA". "</br>"; if (!get_magic_quotes_gpc()) { $ClassSize = addslashes($ClassSize); } @ $db = new mysqli('localhost', 'root', '', 'mylifestyle'); if (mysqli_connect_errno()) { echo "Error: Could not connect to database. Please try again later."; exit; } for($i=0;$i <$num_results;$i++){ $del_id = $radio[$i]; $query = "UPDATE course SET ClassSize = ClassSize -1 Where id='$del_id'"; $result = $db->query($query); if ($result) { echo "u r reg!<br />"; } $db->close(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/249933-passing-value-using-radio-button-and-update-the-database/#findComment-1282811 Share on other sites More sharing options...
Pikachu2000 Posted October 27, 2011 Share Posted October 27, 2011 Same problem. Variables aren't automatically assigned from the $_POST array. You should be developing with the directives: error_reporting = -1 display_errors = On in your php.ini file so errors such as these are reported. Quote Link to comment https://forums.phpfreaks.com/topic/249933-passing-value-using-radio-button-and-update-the-database/#findComment-1282852 Share on other sites More sharing options...
PaulRyan Posted October 27, 2011 Share Posted October 27, 2011 My 2p... It seems to me as though you are copying and pasting code all over the place, you need to re-look at the Time Table file and remove the nested form element start. Also, you shouldn't be passing the class size ($row['ClassSize']) to the file "haha.php", you should in fact pass the class ID. Once you have sent the class ID, you should then update the corresponding record set and decrease the class size by 1. Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/249933-passing-value-using-radio-button-and-update-the-database/#findComment-1282856 Share on other sites More sharing options...
moonjazz Posted October 30, 2011 Author Share Posted October 30, 2011 Hello... i have done some changes i do a GET to see whats the value of ClassSize and HeadCount but each time after i hit the register button the url shows http://localhost/xampp/1/conreg.php?radio=COM01&Submit=Register&ClassSize=&HeadCount= ClassSize and HeadCount has no value... May i know whats causing the problem ? thanks very much <html> <head> <title>TimeTable</title> </head> <body> <h1> Here is the timetable</h1> <?php @ $db = new mysqli('localhost', 'root', '', 'mylifestyle'); if (mysqli_connect_errno()) { echo 'Error: Could not connect to database. Please try again later.'; exit; } $query = "SELECT course.CourseCode, course.CourseName , course.Instructor , course.ClassSize , courseschedule.HeadCount , courseschedule.Date ,courseschedule.Time , courseschedule.Location FROM course INNER JOIN courseschedule ON course.CourseID = courseschedule.CourseID"; $result = $db->query($query); $num_results = $result->num_rows; echo "<table border='1'> <tr> <th>CourseID</th> <th>Course Name</th> <th>Instructor</th> <th>HeadCount</th> <th>Class size</th> <th>Date</th> <th>Time</th> <th>Location</th> <th>Register</th> </tr>"; ?> <?php while($row = $result->fetch_assoc()) { ?> <tr> <form name="form1" method="get" action="conreg.php"> <td><? echo $row['CourseCode']; ?></td> <td><? echo $row['CourseName']; ?></td> <td><? echo $row['Instructor']; ?></td> <td><? echo $row['HeadCount']; ?></td> <td><? echo $row['ClassSize']; ?> </td> <td><? echo $row['Date']; ?></td> <td><? echo $row['Time']; ?></td> <td><? echo $row['Location']; ?></td> <td align='center'><input name='radio' type='radio' value=<? echo $row['CourseCode'];?> onClick='document.Submit.formVar.value='.$row['CourseName'].'> </td> </tr> <?php } $db->close(); ?> <td><input type="submit" name="Submit" value="Register"></td> <input type="hidden" name="ClassSize" value="<? echo $row['ClassSize']; ?>"/> <input type="hidden" name="HeadCount" value="<? echo $row['HeadCount']; ?>"/> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/249933-passing-value-using-radio-button-and-update-the-database/#findComment-1283366 Share on other sites More sharing options...
moonjazz Posted October 30, 2011 Author Share Posted October 30, 2011 any one? Quote Link to comment https://forums.phpfreaks.com/topic/249933-passing-value-using-radio-button-and-update-the-database/#findComment-1283486 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.