简体   繁体   中英

Submit a form from a link and get $_Post value

I have the following problem. I have a form containing several links. Those links should assign a hidden field and submit the form using some javascript.

I'm using PHP 5.4.

When I submit the form using the button, the $_POST containts data. But when the form is submitter from javascript, the $_POST is empty.

Any clue?

<head>
   <script language="text/javascript">
    function affect(numMet)
    {
        document.forms['frmDemo'].num_met_choose.value = numMet;
        document.forms['frmDemo'].submit();
    }
    </script>
 </head>
 <body>

 <?php
   var_dump($_POST);
  ?>

 <form name="frmDemo" enctype="multipart/form-data" method="POST">
   <input  name="num_met_choose" id="num_met_choose">
   <a href="" onClick="javascript:affect('22');" >test 1</a>
   <a href="" onClick="javascript:affect('12');" >test 2</a>
   <button id="submit" type="submit">OK</button>
 </form>

You have two problems.

The first is this:

document.forms['frmDemo'].submit();

combined with this:

<button id="submit" type="submit">OK</button>

By having a form control with id="submit" inside the form, you overwrite the submit property of the the form object. Instead of being a function that you can call, it becomes a reference to the element with the id submit .

You need to use a different id (or none at all since you don't seem to be using it for anything).

You should have got an error in your browser's JavaScript console. Do keep and eye on that when your JS isn't behaving as you expect.

Even if you fix that, you have another problem:

<a href="" onClick="javascript:affect('22');" >test 1</a>
  1. The JavaScript runs
  2. The form is about to submit
  3. The link is followed so the form isn't submitted after all

Don't use a link as a thing to run JS (unless you want it to function as a link, or are setting it up to use the link as a fallback).

Use a button (with type="button" ) instead.


That said. Don't use JavaScript at all for this. You can get the same effect with plain HTML.

 <form enctype="multipart/form-data" method="POST">
    <button type="submit" name="num_met_choose" value="22">test 1</button>
    <button type="submit" name="num_met_choose" value="12">test 2</button>
 </form>
<form action="#" method="post">
   <input type="text"  name="num_met_choose" id="num_met_choose">
   <button type="submit" formaction="/action_page.php">Submit using 
   link</button>
</form>
    <head>
   <script language="text/javascript">
    function affect(numMet)
    {
        //document.forms['frmDemo'].num_met_choose.value = numMet;
        //document.forms['frmDemo'].submit();
        document.getElementById("num_met_choose").value=numMet;
        document.getElementById("frmDemo").submit();
    }
    </script>
 </head>
 <body>

 <?php
   var_dump($_POST);
  ?>

 <form name="frmDemo" enctype="multipart/form-data" method="POST">
   <input  name="num_met_choose" id="num_met_choose">
   <a href="#" onClick="affect('22');" >test 1</a>
   <a href="#" onClick="affect('12');" >test 2</a>
   <button id="submit" type="submit">OK</button>
 </form>

Try this and let me know what is the outcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM