Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
57
rated 0 times [  58] [ 1]  / answers: 1 / hits: 23265  / 12 Years ago, sat, march 10, 2012, 12:00:00

I’m trying to submit a form by clicking links, without using a submit button. Each element of the form has its value which must be submitted when clicked. It doesn’t work. I could use JavaScript or jQuery. Please take a look on the code:



<head>
<style>
div {
width: 100px;
height: 100px;
padding: 10px;
margin: 10px;
background-color: #EEEEEE;
}
</style>
</head>
<body>
<form name=myform action=test.php method=post>
<div>
<a href=javascript: submitform() value='h1'>h1</a><br>
<a href=javascript: submitform() value='h2'>h2</a><br>
<a href=javascript: submitform() value='h3'>h3</a><br>
</div>
</form>
<script type=text/javascript>
function submitform()
{
document.myform.submit();
}
</script>
<div>
<?php
$form=$_POST['myform'];
echo $form.' bla<br>';
?>
</div>
</body>
</html>


with ofir baruch suggestions- works!!!



<head>
<script src=jquery.js type=text/javascript></script>
<style>
div {
width: 100px;
height: 100px;
padding: 10px;
margin: 10px;
background-color: #EEEEEE;
}
</style>
</head>
<body>
<form name=myform action=test.php method=post>
<input type='hidden' id='hx' name='hx' value=''>

<div>
<a href=javascript: submitform('h1')>h1</a><br>
<a href=javascript: submitform('h2')>h2</a><br>
<a href=javascript: submitform('h3')>h3</a><br>
</div>
</form>
<script type=text/javascript>
function submitform(val)
{
$(#hx).val(val);
document.myform.submit();
}
</script>
<div>
<?php
echo $_POST['hx'];
?>
</div>
</body>
</html>

More From » php

 Answers
11

Add the next line after the <form...>:



<input type='hidden' id='hx' name='hx' value=''>


Then , change your calling function to:



<a href=javascript: submitform('h1')>h1</a><br>


Your function:



<script type=text/javascript>
function submitform(val)
{
$(#hx).val(val);
document.myform.submit();
}
</script>


in your php:



echo $_POST['hx'];


How it works?



1.You click a link



2.The js function changes the value of the HIDDEN INPUT field



3.The js function submit the form



4.$_POST relates to the hidden input value


[#86936] Friday, March 9, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rocioblancac

Total Points: 699
Total Questions: 96
Total Answers: 108

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
;