admin管理员组

文章数量:1278720

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>

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>
Share Improve this question edited Mar 10, 2012 at 15:16 user985456 asked Mar 10, 2012 at 13:07 user985456user985456 911 gold badge2 silver badges5 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 3

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

First off, I wouldn't use href="javascript:submitForm();", instead use href="#" onclick="submitForm(this)"

Add a hidden field to your form

<input type="hidden" value="" id="hiddenId" />

In your submitForm method, change myform to forms[0]. I believe that will be more cross-browser friendly. Anywho... add a parameter to your method so you can set the hidden field value.

<script type="text/javascript"> 
function submitform(el) 
{   
  document.getElementById('hiddenId').value = el.value;
  document.forms[0].submit(el); 
} 
</script> 
<a href="javascript: submitform('h1')">h1</a>

function submitform(val)
{
    $.post('.', { myform: val });
}
<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script>
            $(function() {
                $("form a").click(function() {
                    var value = $(this).data('value');
                    $.post('.', { myform: value });
                    return false;
                });
            });
        </script>
    </head>
    <body>
        <form>
            <a href="" data-value="h1">h1</a>
            <a href="" data-value="h2">h2</a>
            <a href="" data-value="h3">h3</a>
        </form>
    </body>
</html>

本文标签: phpSubmit a form by clicking linkswithout using a submit buttonStack Overflow