Page 1 of 1

Post data using Link

Posted: Fri Feb 01, 2008 4:19 am
by Madhum
Can someone tell me how to insert data to the database by using Link.

<a href=\"javascript:document.personal.submit();\"><img src=\"images/mail_one.gif\"> Save</a>

i have above coding and it's working.but when i press Refresh button and while loding the page it's adding another record to the database.

pls help me to solve this :P :(

Re: Post data using Link

Posted: Fri Feb 01, 2008 7:33 pm
by califdon
I don't see how you can add anything to a database using client side script, like Javascript. All the database interaction must be in a server side script, such as php, but you haven't shown any php, so there's nothing to comment on.

Re: Post data using Link

Posted: Fri Feb 01, 2008 8:13 pm
by Zoxive
What that javascript is doing is Submitting the form "personal".

So if you have something like..

Code: Select all

<form name="personal" action="postdata.php" method="post">
<input type="hidden" name="Data1" = value="Value1"/>
<input type="hidden" name="Data2" = value="Value2"/>
</form>
<a href="javascript&#058;document.personal.submit();"><img src="images/mail_one.gif"> Save</a>
 
&#058; = :

Then it would post that to postdata.php with..

postdata.php

Code: Select all

var_dump($_POST);
/*
array(2) {
  ["Data1"]=>
  string(6) "Value1"
  ["Data2"]=>
  string(6) "Value2"
}
*/
 

Re: Post data using Link

Posted: Mon Feb 04, 2008 8:45 pm
by Madhum
Dear Califdon and Zoxive,

This the coding that i have and it's wrking.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?
include("connect.php");
$aa=$_POST['aa'];
$bb=$_POST['bb'];
$sql_file = "INSERT INTO ww (aa,bb) VALUES('".$aa."','".$bb."')";
$reg_result = mysql_query($sql_file) or die("error".mysql_error());
?>
<body>
<form name="MyForm" method="POST" action="">
<input name="aa" type="text" id="aa" />
<input name="bb" type="text" id="bb" />
</form>
<?
echo '<div id="sub_menu_div" >';
echo "<ul id=\"cssdropdown\">";
echo "<li class=\"mainitems\"><a href=\"javascript:document.MyForm.submit();\"><img src=\"images/mail_one.gif\"> Save</a></li>";
echo "</ul>";
echo "</div>";
?>
<!--<a href="javascript:document.MyForm.submit();">
Click to submit the form
</a>-->

</body>
</html>

my problem is,when i press Refresh and when i load the page it's adding empty record or previous record to the database.

Re: Post data using Link

Posted: Tue Feb 05, 2008 7:16 am
by sastha
Unconditionally,records get inserted into DB.
Try to put some conditions like..
<?
include("connect.php");
if(isset($_POST['hidden_field'])){
$aa=$_POST['aa'];
$bb=$_POST['bb'];
$sql_file = "INSERT INTO ww (aa,bb) VALUES('".$aa."','".$bb."')";
$reg_result = mysql_query($sql_file) or die("error".mysql_error());
}
?>

Put a hidden field.

<form name="MyForm" method="POST" action="">
<input name="aa" type="text" id="aa" />
<input name="bb" type="text" id="bb" />
<input name="hidden_field" type="hidden" id="hide_me" />

</form>

Hope this do will do the job.