...

View Full Version : Whats the problem with the link and button



mwm
11-27-2006, 10:42 PM
Just wrote a quick script to see if a variable is being passed. This works ok when using the button, but the variable won't pass using the link. Any idea why?



<html>
<form action="2.php" method="post">
<input type="hidden" name="id" value="mike">
<input type="submit">
</form>
</html>



2.php
<html><form action="3.php" method="post">
<?
echo "<input type=hidden name=id value='$id'>";
echo "Variable $id has passed";
?>
<br>
<input type="submit">
</form>
</html>



<html><form action="4.php" method="post">
<?
echo "<input type=hidden name=id value='$id'>";
echo "Variable $id has passed twice";
?>
<br>
<a href="4.php">Click here to see if variable will pass </a>
<br>
<br>
<input type="submit"></a>
</form>
</html>



<html>
<form>
<?
$id=$_POST['id'];
echo "<input type=hidden name=id value='$id'>";
if (isset($_POST['id'])){
echo "YES! variable $id has pass to a third page via a Link";
} else {
echo "Nope variable did not pass";
}
?>

</form>
</html>

CFMaBiSmAd
11-27-2006, 11:12 PM
A link in a form is just a link. Clicking it does not cause the form to be submitted.

To pass a value this way, there are some different choices -

1) You can pass it as part of the action= URL. It will be available on the next page as a $_GET variable (you can pass parameters on the action = URL using the GET method and pass form fields using the POST method in the same form.)

2) If you only want to use a link without this being part of a form and pass parameters as part of the URL, you can create a href = link similar to what you have, append the parameters, and they will be available on the next page as $_GET variables.

3) You can cause a form to be submitted using a link by using an onclick event to submit the form. This seems to be what you are trying by placing the link within your form.

mwm
11-28-2006, 02:03 PM
Thank you for your response. Would you be willing to show me a brief example of each of the 3 ways.

Thanks again

mwm
11-28-2006, 04:00 PM
Anyone?

Thank You

mlseim
11-28-2006, 04:58 PM
This method has one problem ... anyone can see $id by looking at the source code.
If $id is a sensitive or secret value, you should be using sessions instead.



<html>
<form action="2.php" method="post">
<input type="hidden" name="id" value="mike">
<input type="submit">
</form>
</html>




2.php
<?php
$id=$_REQUEST['id'];
?>
<html><form action="3.php" method="post">
<input type="hidden" name="id" value="<?=$id?>"><br>
Varible to pass is: <?=$id?><br>
<input type="submit">
</form>
</html>




<?php
$id=$_REQUEST['id'];
?>
<html><form action="4.php" method="post">
<input type="hidden" name="id" value="<?=$id?>"><br>
Varible to pass is: <?=$id?><br>
<input type="submit"></a>
</form>
</html>




<html>
<?php
$id=$_REQUEST['id'];
if (isset($id)){
echo "YES! variable $id has passed to a third page via a Link";
} else {
echo "Nope variable did not pass";
}
?>
</html>

CFMaBiSmAd
11-28-2006, 05:45 PM
Anyway, here are working examples of the three different ways mentioned -

<?php
// method 1, form code -
$id = "mike";
?>
<form action="f1b.php?id=<?php echo $id;?>" method="post">
Enter some name: <input type="text" name="name">
<input type="submit">
</form>

<?php
// method 1, action= code, f1b.php -
echo "The id is: {$_GET['id']}<br />";
echo "The entered name is: {$_POST['name']}";
?>

<?php
// method 2 code -
$id = "mike";
echo "<a href=\"f2b.php?id=$id\">Click here to see if variable will pass </a>"
?>

<?php
// method 2, target code, f2b.php -
echo "The id is: {$_GET['id']}";
?>

<?php
// method 3, form code -
$id = "mike";
?>
<form name="userform" action="f3b.php" method="post">
<input type="hidden" name="id" value="<?php echo $id;?>">
Enter some name: <input type="text" name="name">
</form>
<a href=# onclick="document.userform.submit()">Click to Submit</a>Note: Because the onclick refers to the form by its name="..." name, you can put the link in the form or outside the form...


<?php
// method 3, action= code, f3b.php -
echo "The id is: {$_POST['id']}<br />";
echo "The entered name is: {$_POST['name']}";
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum