Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/267221-updating-db-with-php-and-ajax/
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.