954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Submit data through link??

<?
$sql="select * from table";

while($row=mysql_fetch_array($sql))
{
        $name=$row['name'];
	$view=$row['view'];
	$view1=$row['view1'];
        $id2=$row['id'];
?>
<form name="myform" action="list.php?id=<?=$id2;?>&view=<?=$view;?>" method="post">
 <td align="center" width="5%"><?=$name;?></td>
 <td align="center" width="5%"><?=$view;?></td>
<td align="center" width="3%"><input type="text" value="<?=$view1; ?>" size="5" id="newva" name="newva"> </td>
</form>
 <td align="center" width="5%">
<a href="/show/list?id=<?=$id2;?>&view=<?=$view;?>" onClick="document.myform.submit();" >Submit</a>
</td>

<? } ?>


It shows out put like
name view view1 link
xxx 4 4 (in text box) Submit
yyy 5 5(in text box) Submit
zzz 6 6(in text box) Submit

I want to edit value for view1 and when click on link submit it saves
edited value in table(mysql).

How to do that??

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

<?
$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;
}


?>

mr.khurrams
Newbie Poster
13 posts since Oct 2008
Reputation Points: 10
Solved Threads: 2
 

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.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

can you give me prototype.js and myAjax.js

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

thats more good, will you suggest me some useful links to learn AJAX with PHP?

mr.khurrams
Newbie Poster
13 posts since Oct 2008
Reputation Points: 10
Solved Threads: 2
 

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.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You