Aur1cles Posted August 17, 2012 Share Posted August 17, 2012 I have a form where users can change previously entered data, it get's submitted when they click on check box. I got some of it to work, right now it only knows which entry to update and passes one value, but I need it to pass two check box values and one input value. This is the form: <input type="checkbox" name="check1" value="1"/>//need to get this value to database <input type="checkbox" name="check2" value="1"/>//need to get this value to database <input name="comment[<?PHP echo $id; ?>]" type="text" size="20" maxlength="200"/> //need to get this value to database <input type="checkbox" onclick="SaveToDatabase(<?php echo $id;?>)" value="1"/> // when this is checked the data gets updated Ajax: function SaveToDatabase(id) { var url = "update_database.php?id="+id; if(window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if(window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); } req.open("GET", url, true); req.send(null); } And PHP file that updates the database : include('mysql.php'); $id = $_GET['id']; $sql = "UPDATE database SET processed = '1' WHERE id = '$id'"; $result = mysql_query($sql) or die (mysql_error()); If somebody could help me find a way to pass all these values to ajax and then to php for update. Quote Link to comment https://forums.phpfreaks.com/topic/267221-updating-db-with-php-and-ajax/ Share on other sites More sharing options...
Christian F. Posted August 17, 2012 Share Posted August 17, 2012 I recommend that you look into jQuery for this, as it'll help you do this a LOT easier. If I recall correctly, they do have some examples showing how to accomplish what you're after in their manual too. Quote Link to comment https://forums.phpfreaks.com/topic/267221-updating-db-with-php-and-ajax/#findComment-1370128 Share on other sites More sharing options...
ialsoagree Posted August 17, 2012 Share Posted August 17, 2012 Your problem is here: "var url = "update_database.php?id="+id;" The URL only contains the ID, it doesn't contain the names and values of the form. Because you're not using the HTML submit feature (which would have the browser handling the HTTP request to submit all the form names and their values) you have to do this manually. That is, you have to rewrite the URL to include every form name, and the form value. For example: var url = "update_database.php?id="+id; var url = url+"&check1="+ //assign an ID to check1, and then you could use getElementById(ID Assigned).value Quote Link to comment https://forums.phpfreaks.com/topic/267221-updating-db-with-php-and-ajax/#findComment-1370160 Share on other sites More sharing options...
jardrake Posted August 19, 2012 Share Posted August 19, 2012 Ugh, why do we still dwell in the world of manually typing our XMLHttpRequest Objects, when jQuery is so much easier. Quote Link to comment https://forums.phpfreaks.com/topic/267221-updating-db-with-php-and-ajax/#findComment-1370726 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.