Post data using Link

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Madhum
Forum Newbie
Posts: 7
Joined: Fri Feb 01, 2008 3:40 am

Post data using Link

Post 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 :(
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Post data using Link

Post 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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Post data using Link

Post 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"
}
*/
 
Madhum
Forum Newbie
Posts: 7
Joined: Fri Feb 01, 2008 3:40 am

Re: Post data using Link

Post 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.
sastha
Forum Newbie
Posts: 6
Joined: Tue Feb 05, 2008 3:42 am

Re: Post data using Link

Post 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.
Post Reply