3
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by diafol
0
<?
$rs = mysql_query("select * from test where id = '".$_GET['id']."'");///////////////////you have to send this $_GET['id'] using any GET method for this page
while($row=mysql_fetch_array($rs))
{
        $name=$row['name'];
        $id2=$row['id'];
?>
<form name="myform" action="list.php" method="post">
 <td align="center" width="5%"><input type="text" value="<? echo $name; ?>" name="name" /></td>
 <input type="hidden" name="up_id" value="<? echo $id2;?>"/>
 <td align="center" width="5%"><input type="submit" value="update"/></td>
</form>
<? }
if(isset($_POST[up_id))
{
	$query = mysql_query("update table set name = '".$_POST['name']."' where id = '".$_POST['up_id']."'")
	echo "Record has been Updated Successfully...........";
	exit;
}


?>
0

You're already using js for submitting, why not use ajax?

IN THE FORM PAGE HEAD SECTION:

<script src="/path/to/prototype.js" type="text/javascript"></script>
<script src="/path/to/myAjax.js" type="text/javascript"></script>

YOUR FORM:

<input id="names" name="names" type="text" /> <a href="#" onclick="update_db('names');return false;">Update</a> 
<input id="view" name="view" type="text" /> <a href="#" onclick="update_db('view');return false;">Update</a> 
<input id="view1" name="view1" type="text" /> <a href="#" onclick="update_db('view1');return false;">Update</a> 
<hidden id="myId" name="myId" value="<?=$id2;?>" />

IN YOUR myAjax.js FORM:

function update_db(updateField){
  var sField = updateField;
  var sVal = $F(updateField);
  var sId = $F('myId');
  var pars = "uf=" + sField + "&id=" + sId + "&value=" + sVal;
  var phpupdater = "includes/formupdater.php"; 
  var oAjax = new Ajax.Request(phpupdater,{method: 'post',parameters: pars,onSuccess: function(transport){
      alert("Success!"); 
    }});
}

In YOUR formupdater.php FILE:

$id = $_POST['id'];$f = $_POST['uf'];$val = $_POST['value'];

$sql ="UPDATE yourtable SET {$f} = '{$val}' WHERE user_id = '{$id}'"; 

(then connect and update your usual way)

Normally I'd escape and validate all data (client side and server side), but moitted for brevity. Prototype library can be downloaded with Scriptaculous. Note this code has not been run - off top of my head. You'll probably need to debug.

0

For myAjax.js - that's the code you need to put in it:


IN YOUR myAjax.js FORM (sorry FORM should be FILE):

function update_db(updateField){
  var sField = updateField;
  var sVal = $F(updateField);
  var sId = $F('myId');
  var pars = "uf=" + sField + "&id=" + sId + "&value=" + sVal;
  var phpupdater = "includes/formupdater.php"; 
  var oAjax = new Ajax.Request(phpupdater,{method: 'post',parameters: pars,onSuccess: function(transport){
      alert("Success!"); 
    }});
}

The prototype.js file can be downloaded from the scriptaculous site or from http://www.prototypejs.org.

As to where you can learn Ajax:
I bought a few books on the subject (Beginning AJAX and Professional AJAX from WROX). If you Google Ajax tutorials, I'm sure you'll find something useful. However, if (like me) your JS is a bit dodgy, use prototype or another framework to take the sting out of creating ajax objects.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.