PHP - How To Post Checkbox Data Into A New Table.
Hey
I have checkbox and when the user tick one or more box these should be stored as 0 or 1 but since its while loop, I am not sure how to work that out. So far I have <html> <?php session_start(); include '../Database/connection.php'; $Value24 = mysql_real_escape_string(trim($_POST['myselect'])); $_SESSION['smodule'] = $Value24; ?> <body> <form action="Test_Completed.php" method="post"> <?php $query = mysql_query(" SELECT * FROM Test WHERE Tes_ID = '{$_SESSION['smodule']}' "); while( $query1 = mysql_fetch_array($query) ) { echo "Test Name: {$query1['Tes_Name']}"; } ?> <br> <?php $query = mysql_query(" SELECT * FROM User WHERE Use_ID = '{$_SESSION['ssubject']}' "); while( $query1 = mysql_fetch_array($query) ) { echo "User Name: {$query1['Use_Name']}"; } ?> <br> <?PHP include '../Database/take_an_exam.php'; $intNumber = 1; while($info = mysql_fetch_array( $sql )) { echo "$intNumber, {$info['Que_Question']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice1[]\" value=\"{$info['Que_Choice1']}\" /> "; echo "{$info['Que_Choice1']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice2[]\" value=\"{$info['Que_Choice2']}\" /> "; echo "{$info['Que_Choice2']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice3[]\" value=\"{$info['Que_Choice3']}\" /> "; echo "{$info['Que_Choice3']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice4[]\" value=\"{$info['Que_Choice4']}\" /> "; echo "{$info['Que_Choice4']} <br />\n"; $intNumber++; } ?> <input type="submit" value="submit"/> </body> </html> </body> </html> <?PHP $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("Examination", $con); //Get & clean value from POST data $choice1 = mysql_real_escape_string(trim($_POST['choice1'])); $choice2 = mysql_real_escape_string(trim($_POST['choice2'])); $choice3 = mysql_real_escape_string(trim($_POST['choice3'])); $choice4 = mysql_real_escape_string(trim($_POST['choice4'])); $user = mysql_real_escape_string(trim($_SESSION['username1'])); //Create and run INSERT query $query = "INSERT INTO Answer (`Ans_Answer1`, `Ans_Answer2`, `Ans_Answer3`, `Ans_Answer4`, `Que_ID`, `Use_ID`) VALUES ('{$choice1}', '{$choice2}', '{$choice3}', '{$choice4}', '{$query1}', '{$user}')"; $result = mysql_query($query) or die (mysql_error()); $_SESSION['Ans_ID'] = mysql_insert_id(); header("location:check.php"); ?> Similar TutorialsDoes anyone no a way to post a unchecked checkbox value? $post='{"cart_items":[{"configuration":{"price":100,"recharge_number":"9999999999"},"product_id":"999","qty":1}]}';i try this n reslut was :There are no valid items in cart: help me plz Edited by ShivaGupta, 30 November 2014 - 01:11 AM. Trying to fix existing some code (complete beginner here) on the company I work for website. There's a check-box which when ticked should apply delivery charges and if not it shouldn't - currently it's working in that on the same page it calculates the total cost correctly when checked or not checked but when passing on information it always passes on the delivery charge even if un-ticked... The simplest thing I can see would be to put an if statement in to say if (not ticked) then del_charges = 0.00. Any idea how I can do this in this instance? Code: [Select] <?php // add a checkbox for delivery charges echo '<span class="form"><label>Delivery Charges</label>'; echo '<input type="checkbox" name="del_charges" id="del_charges" value="1" class="boxes" '; if ( isset($_POST['del_charges']) && !empty($_POST['del_charges']) ) echo 'checked="checked"'; echo '/>'; ?> Thanks. i have checkbox in a table and want to put the value as the id in the database then when i press submit it displays all the row data for each checkbox i want to insert only whats checked to db:) but everything get posted for now i came upwith a for loop but thats for inserting all data not only whats checked any one know a good way to do this? Hi. I want to put data from these checkboxes (example): Jan (value = jan) Chris (value = chris) Tom (value = 50) Jon (unchecked) Put janchris50 to the database after finding correct row with user ID, which is user_id for this example. Then, the user goes to another page and he should see this: Jan (checked) Chris (checked) Tom (checked) Jon (unchecked) In other words - page should get the information from the database and check the checkboxes that appears to be checked in the database. By the way, it should rewrite the row each time, as the user can modify it. Thanks. Hello all I am working on creating a basic ordering form that allow the user to use check-boxes to select certain products and than return a total back to them. I am having a hard time figuring out the php code to gather and calculate things. What I dont quite understand is how to pass the data properly so my $sum calculation actually knows what the values are... In my html doc I have my checkboxes as follows: //formatted in tables <input type = "checkbox" name = "eight100wattreg" size ="40" /> <input type = "checkbox" name = "four100wattreg" size ="40" /> //etc.... and in my php doc: <?php> //get form values $four100wattreg = $_POST["four100wattreg"]; $eight100wattreg = $_POST["eight100wattreg"]; $four100wattlong = $_POST["four100wattlong"]; $eight100wattlong = $_POST["eigth100wattlong"]; $payment = $_POST["payment"]; //set lightbulb values $four100wattreg=2.39; $eight100wattreg=2.39; $four100wattlong=2.39; $eight100wattlong=2.39; //calculate costs if (isset($_POST['four100wattreg']) { //checkbox is checked $four100wattreg = $_POST['four100wattreg']; } else if (!isset($_POST['checkbox']) { //checkbox not checked $four100wattreg = 0; } $sum = $four100wattreg + $eight100wattreg + $four100wattlong + $eigth100wattlong; ?> <?php> echo "Your total is: '$sum' <br />"; ?> I also attached the files if someone wants to look at them in they're entirety. Any advice would be greatly appreciated, but please keep in mind I am a php beginner and am still learning. Thanks, Charlie helo does anyone know how to display data from db in checkbox form? i have been searching for days and still cannot find answer that i can understand. i hope someone can help me. i really am lost. any help is much appreciated I've been poking around online for about a day now trying to find a good way to do the following: I have an "order" I'm reading in from a MySQL database (not completely relevant, but just fyi). And, for this order there are a number of "parts" (automotive parts) that have been ordered. For each part there is data pertaining to the quantity of parts that have been ordered and the number of parts that have been allocated (based on our inventory) and then a third column that has the number of parts still in that order that are still to be allocated at a future date. Basically, I'm displaying this data in a form with each row having a checkbox to the far left. What I want to do is allow the user to check any number of check boxes. If a check box is checked the form should submit all of the input text boxes for that row (the user can update the quantities) that I talked about in the second paragraph for that "part" (each part is displayed in a row and has a checkbox). Unfortunately, most of the examples I found for checkboxes in a form submit a specific value (like a number of a letter). I need the form to submit the values that are in the textboxes. So basically, I'm thinking I need the page to update a PHP object whenever the value of a textbox is changed and then when the user clicks a button it will send the php object for each checked row through GET or POST. But, how would I get it to onchange update a PHP object (the PHP object would need to be a 2-D array where the first index allows you to select the part and the second index allows you to select which quantity you will change). I'm thinking I would need to call a script from the onchange like: onchange="somePHPfunction(partNumberIndex, whichQuantityI'mChanging, currentValueOfThatQuantity)" but of course that would require me to embed some php inside the onchange call since the partNumberIndex and whichQuantity are PHP variables.. Any ideas on how to do this sort of thing? Or any ideas on how to do this differently? I'm trying to avoid javascript because it would place a javascript constraint on the users. But, maybe there's no other way to do this sort of thing? Any help would be greatly appreciated! Hey phpFreaks, im having some troubles getting my script to work correctly and im also not sure if this issue is in the right section of the forum. but heres what i have going on. I have a query result that displays a list of images with a checkbox and a couple buttons for edit and delete. everything works fine other than the checkbox stuff. I had it working when i was using a submit button, but i wanted to get rid of that button cuz it was only dealing with the checkboxes. so whats going on when using the checkboxes is that when checked or uncheck it would submit the form. It works to submit but its not submitting any data to the database. heres what i got for code for this section of checkbox. Code: [Select] <?php echo '<form method="post">'; if(isset($_POST['submit'])){ foreach($_POST['id'] as $id){ $value = (isset($_POST['location'][$id]) && $_POST['location'][$id]=="0" ? '0' : '1'); $insert = mysql_query("UPDATE items SET location='$value' WHERE id='$id'") or die('Insert Error: '.mysql_error()); } } $result = mysql_query("SELECT * FROM items") or die("Query Failed: ".mysql_error()); $counter = 0; echo '<div class="specialcontainer">'; while($row = mysql_fetch_array($result)){ list($id, $item_info, $item_img, $price, $sale, $location) = $row; if($location == '0'){ $set_checked = 'checked="checked"'; }else{ $set_checked = ''; } if($counter % 5==0) { echo '</div>'; echo '<div class="specialcontainer">'; } echo '<div class="special"><img src="../images/items/'.$item_img.'" width="130" /><br />'.$item_info.'<br />$'.$price.'<br />$'.$sale.'<br />Slide Show: <input type="checkbox" id='.$id.' value="0" name="location['.$id.']" '.$set_checked.' onchange="this.form.submit()"/><br /><input type="button" value="Edit" name="edit" id="'.$id.'" onclick="window.location.href=\'specials.php?action=edit&id='.$id.'\'"><input type="button" value="Delete" name="Delete" id="'.$id.'" onclick="window.location.href=\'specials.php?action=delete&id='.$id.'\'"><input type="hidden" name="id[]" value='.$id.' /></div>'; $counter++; } echo '</div>'; echo '</form>'; ?> I am retrieving data from mysql and printing this as a table. Now I want to print this as a checkbox. But not sure how to? <?php mysql_connect("localhost", "root", "")or die("cannot connect"); mysql_select_db("Examination")or die("cannot select DB"); $sql=mysql_query("SELECT * FROM question") or die(mysql_error()); Print "<table border cellpadding=5>"; while($info = mysql_fetch_array( $sql )) { Print "<th>Question:</th> <td>".$info['Que_Question'] . "</td> "; Print "<th>Choice1:</th> <td>".$info['Que_Choice1'] . "</td> "; Print "<th>Choice2:</th> <td>".$info['Que_Choice2'] . "</td> "; Print "<th>Choice3:</th> <td>".$info['Que_Choice3'] . "</td> "; Print "<th>Choice4:</th> <td>".$info['Que_Choice4'] . "</td> "; } Print "</table>"; ?> Hi guys, I think I may be losing my marbles over this form I've put together. I'm trying to get my code to post the following to a database but my MySQL and PHP isn't that good, so wondering if any one could possibly suggest what I could do? Here is my code: Code: [Select] <?php // we check if everything is filled in if(empty($_POST['fname']) || empty($_POST['lname']) || empty($_POST['email'])) { die(msg(0,"All the fields are required")); } // is the sex selected? if(!(int)$_POST['sex-select']) { die(msg(0,"You have to select your sex")); } // is the birthday selected? if(!(int)$_POST['day'] || !(int)$_POST['month'] || !(int)$_POST['year']) { die(msg(0,"You have to fill in your birthday")); } // is the email valid? if(!(preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['email']))) die(msg(0,"You haven't provided a valid email")); // I think here is where I need to put something to get the data into a table! echo msg(1,"registered.html"); function msg($status,$txt) { return '{"status":'.$status.',"txt":"'.$txt.'"}'; } ?> How do I get the $_POST['day'] and $_POST['month'], $_POST['year'] etc into a table? It should be so easy but my head is hurting from trying lots of things and tutorials online. Any thoughts my PHP Freaks? im trying to understand this Post Data script it's not posting, no errors: postdata.php Code: [Select] <?php //create array of data to be posted //$post_data['firstName'] = 'Name'; $post_data['item_name'] = '12345'; //traverse array and prepare data for posting (key1=value1) foreach ( $post_data as $key => $value) { $post_items[] = $key . '=' . $value; } //create the final string to be posted using implode() $post_string = implode ('&', $post_items); //we also need to add a question mark at the beginning of the string $post_string = '?' . $post_string; //we are going to need the length of the data string $data_length = strlen($post_string); //let's open the connection $connection = fsockopen('www.example.com', 80); //sending the data fputs($connection, "POST /i.php HTTP/1.1\r\n"); fputs($connection, "Host: www.example.com \r\n"); fputs($connection, "Content-Type: application/x-www-form-urlencoded\r\n"); fputs($connection, "Content-Length: $data_length\r\n"); fputs($connection, "Connection: close\r\n\r\n"); fputs($connection, $post_string); //closing the connection fclose($connection); ?> reuslt should be posted here i.php Code: [Select] <?php require('database_connection.php');?> <?php if(isset($post_items[0])) { //$Subscription_id = mysql_real_escape_string($md5c); //$PropertyID = mysql_real_escape_string($rowData['ID']); //$User_ID = mysql_real_escape_string($rowData['User_ID']); $item_name=mysql_real_escape_string($post_items[0]); $query = 'INSERT INTO SP_subscriptions(id,) values("'.$item_name.'")'; //$query = 'INSERT INTO SP_subscriptions(id,PropertyID,User_ID) values("'.$item_name.'","'.$PropertyID.'","'.$User_ID.'")'; $success = mysql_query($query); } else { $item_name=54321; $query = 'INSERT INTO SP_subscriptions(id) values('.$item_name.')'; //$query = 'INSERT INTO SP_subscriptions(id,PropertyID,User_ID) values("'.$item_name.'","'.$PropertyID.'","'.$User_ID.'")'; $success = mysql_query($query);} //var_dump($_POST); //var_dump($query); echo $query; ?> Hi I have a little strange problem. I cannot get the post data from my form. It works fine if there is 10 or 15 user but reaching 40 or more than form values not posting. This is output of size: echo (int) $_SERVER['CONTENT_LENGTH']; ===== 22164 Code: [Select] if(isset($_POST['hiddenField'])) { // do some work; } $sql = mysql_query("SELECT id, name FROM user WHERE shop_id = '" .$_SESSION['id']. "' ORDER BY name ASC"); while($row = mysql_fetch_array($sql)) { echo ' <div style="width:100%; border:solid 1px #CCC; background-color:#f1e7e7; margin-bottom:5px;"> <table class="tableSmall"> <tr><th width="81">' .$row['name']. '</th><th width="71">'; echo $grl_pic .' </th> <td width="622"><table width="100%"> <tr>'; for($j=0; $j<7; $j++) { $sql2 = mysql_query("SELECT user_master_id, user_start, user_end FROM work_master WHERE user_master_id = '" .$row['id']. "' AND date = '" .$days_no_kanji[$j]. "'"); $row2 = mysql_fetch_array($sql2); if(mysql_num_rows($sql2) == 0) { $start = "00:00"; $end = "00:00"; $m = 0; } else { $start = $row2['user_start']; $end = $row2['user_end']; $m = 1; } echo ' <th><input type="hidden" name="userData[]" value="' .$row['id']. '_' .$j .'_' .$m. '" /> <select name="' .$row['id']. '_' .$j .'_' .$m. '_start">' .ShopHour($start). '</select><br />~<br /> <select name="' .$row['id']. '_' .$j .'_' .$m. '_end">' .ShopHour($end). '</select> </th>'; } echo ' </tr> </table></td> </tr> </table> </div>'; } Is there someone has any idea why php is not posting? Hi, having problems getting checkboxes to display all reuslts when a user selects more than one check box say in the category section and one in the location section - see page http://www.partyco.co.uk/event-and-party-venues/ - submit to see reults page: I managed to get it to display reults if the user only seletc either a right or left column option OR one of each - BUT not when thet select multiple categories and one location - and ideas how to do this ? putting it into an array perhaps - but how - new to some of this.... here is the code for the reults page: Code: [Select] <?php $location = $_POST[location]; $category = $_POST[category]; ?> <?php $Link = mysql_connect("xxxxxxxxx", "xxxxxxxxx", "xxxxxxxx") or die(mysql_error()); mysql_select_db("xxxxxxxx") or die(mysql_error()); // selects db listings when location not given if (empty($location)) { $query = "SELECT * FROM venues WHERE category = '$category' order by title"; $result = mysql_query($query) or die(mysql_error()); // selects db listings when category not given } elseif(empty($category)) { $query = "SELECT * FROM venues WHERE location = '$location' order by title"; $result = mysql_query($query) or die(mysql_error()); // selects db listings when both given }else { $query = "SELECT * FROM venues WHERE category = '$category' and location = '$location' order by title"; $result = mysql_query($query) or die(mysql_error()); } while($row = mysql_fetch_array($result)){ echo "<div class=\"resultsShort\" style=\"margin-bottom:10px;\">"; echo "<h2 id=\"resultsHeading\">"; echo $row['title']; echo "</h2>"; echo "<p class\"resultspara\">". nl2br($row['description']). "</p>"; echo "<h4 style=\"margin:5px 0 0 0; padding:0;\">Contact details</h4>"; echo "<p class\"resultspara\">". nl2br($row['contact']). "</p>"; echo "<div style=\"float:left; width:124px; height:40px; margin:10px 15px 0 0;\">"; echo "<a href=\"/party-supplier-resources/email-supplier.php?title=". $row['title']. "&email=" . $row['email']. "&location=" . $row['location']. "&category=" . $row['category']. "\" title=\"contact this venue here\">"; echo "<img src=\"/images/email-supplier.jpg\" width=\"124\" align=\"right\" height=\"35\" alt=\"contact this supplier button\" border=\"0\" /></a>"; echo "</div>"; echo "</div>"; } ?> <?php include("../include/shareLinks.php"); ?> <div id="popupContact"> <a id="popupContactClose" title="close this window">close x</a> <h1>Supplier Directory Enquiry Form</h1> <?php include("../include/enquiryform.php"); ?> </div> <div id="backgroundPopup"></div> <?php mysql_close ($Link); ?> Any help appreciated! Aaron MOD EDIT: [code] . . . [/code] BBCode tags added. i am trying to fit this php code to so when ever i upload a video link to the database it prints in the divs from left to right. ( i have made blank boxes for where the videos should be, so you guys can see what i mean. i will give you the code here. Code: [Select] <?php $query = mysql_query("SELECT * FROM `G4V_Videos` ORDER BY `id` DESC") or die(mysql_error()); while ($data = mysql_fetch_array($query)) { ?> ID-nummer: <?php print $data['id']; ?> - Name: <?php print $data['navn']; ?> - <a href="/Video.php?id=<?php print $data['id']; ?>"><img src="http://i.ytimg.com/vi/<?php print $data['link'];?>/hqdefault.jpg" width="200" height="160" /></a><br /> <a href="/Video.php?=<?php print $data['id']; ?>"> <?php } ?> and the website is here, http://www.game4vids.com/index.php and i also want it so once i have used up all the boxes it posts the new video in box one and overwrites the old one if you get what i mean. This website is a great example of what i mean. http://www.retardo.dk Hi guys, I am creating a quiz system for my personal site, I am done with all the quiz questions creating part, but I am stuck with something like... user taking a random sql generated quiz and submitting it.. Here's randomly generated sql quiz, even the answers are in random order: <form method="post"> <b>1. What is color or sky</b> <ol> <li><input name="question_3_answer" type="radio" value="6" /> sky</li> <li><input name="question_3_answer" type="radio" value="8" />blue</li> <-- true <li><input name="question_3_answer" type="radio" value="3" />red</li> </ol> <b>2. What is name of my cat</b> <ol> <li><input name="question_6_answer" type="radio" value="9" />brat</li> <--true <li><input name="question_6_answer" type="radio" value="7" />rat</li> </ol> <b>3. What is name of my pet</b> <ol> <li><input name="question_18_answer" type="radio" value="9" />cat</li> <--true <li><input name="question_18_answer" type="radio" value="7" />dog</li> </ol> </form> Now this is tricky part for me is.. receiving these data in another page.. since it's randomly generated questions, how do I know which question it is? here's example I've tried : $list_all_quiz_questions = $db->get_row("SELECT question_id, its_true_ans_id FROM question_table WHERE quiz_group = 'ef23fsdfers3e' "); $i = 1; foreach ($list_all_quiz_questions as $myquestion) { $answer.$i == $_POST["question_".$myquestion->question_id."_answer"]; //?????????? not working lol ;(( $i++ } I'm doing an ajax call from javascript using jquery library: Code: [Select] ajaxCall: function(process, obj, type){ $.post("registration-post.php", type + "=" + process, function(data){ if(data==""){ $(obj).css(registration.errorNotificationType, registration.validatedColour); }else{ $('.error').append($.trim(data) + "<br />"); $(obj).css(registration.errorNotificationType, registration.errorColour); registration.progress = false; } } ); } When the ajax calls the php page, I want the php page to know what post information the javascript has sent, rather than pre-defining what the PHP should be trying to retrive from the POST data. Something like // pseudo $_POST["whateverwassentfromajax"] I want to do this because I want to have one function doing everything. So in my javascript I am saying, check if the username and email have already been used... But i dont want to call two different php functions. //pseudo function 1(){ $_POST["username"]; } function 2(){ $_POST["email"]; } Is there a special way to know which post data has been sent. Or do i need to do a few if statements in php. So basically I'm using PHP to solve a math problem for me. The user puts in a few parameters and the program runs those numbers through the algorithm and spits out a bunch of xy coordinates. I'm using the PHP SELF method to retrieve the user input. This all works fine, but it's afterwords that I run into problems. I create a drop down menu of all the x values and I want the user to be able to choose any x value they wish and to have the corresponding y value be displayed. The problem is that I have to use another POST command for this and when I do, it wipes out all the computed data. Of course the y-value is never displayed, and there lies the problem. I'm sure there are many ways to get around this, but I could not find one out myself. Anyone have any ideas? Hello, I need to post plenty of data to a file with a java file with a .do extension using a php program. How do I get to auto-post mass data in php from a database straight to this java file? Put in mind that each mysql record is posted as its own form as in: <form ...>mysql record</form> Thanks in advance |