2

My if(isset) validation is returning false after I have submitted the form through jQuery ,however works fine when done without jquery. Reason I am using jQuery is because I need to submit multiple forms:

Button

<input class="btn btn-primary" type ="submit" id="myButton" 
name="create_record" value="Submit 1">

jQuery:

 <script>
  $(document).ready(function () {

    $("#myButton").click(function (event) {
     event.preventDefault();
     $("#form1").submit();
   //       $("#form2").submit();
   });
  });
  </script>

PHP

<?php
 if(isset($_POST['create_record'])){
   $ecode = $_POST['ecode'];
   $ename = $_POST['ename'];
   $date = $_POST['date'];
   $jobRole = $_POST['jobRole'];
 }else{
   echo "did not receive anything";
 }

 ?>

Always getting "did not receive anything" . Can someone please help.

andy
  • 31
  • 3
  • can u post all your html form please ? – Lyes Dec 10 '18 at 04:45
  • unless you've also modified the submit handler to not redirect/refresh the page, the second form submit might not work as expected anyway. check billynoah's answer +comments for some ideas... – Jakumi Dec 10 '18 at 06:40
  • A dumb suggestion. Try changing `type ="submit"` to `type="button"` – Cerlin Dec 10 '18 at 07:11

3 Answers3

1

The submit button value only gets sent if the form is submitted in the traditional way by a button click. Since you are submitting the form via javascript, you'll need to explicitly include the submit button's value or validate your post data in some other way. If you need the value of the specific button that was clicked, something like this should work:

$("#myButton").click(function (event) {
    event.preventDefault();
    var el = '<input type="hidden" name="' + $(this).prop('name') + '" value="' + $(this).val() + '">';
    $("#form1").append(el).submit();
});

As for your objective of submitting multiple forms at once, I believe it's impossible without using ajax as discussed here. If you need guidance on how to do that, better to open a new question.

  • 1
    correct diagnosis, I believe including the hidden field in the original form by default and removing the name from the submit buttons is possibly preferred though (simpler js code, overall more consistent strategy) – Jakumi Dec 10 '18 at 06:21
  • 1
    @Jakumi - I agree, but it's possible that the op has several submit buttons and needs to capture the value of the one that's clicked into all the forms. – But those new buttons though.. Dec 10 '18 at 06:22
  • 1
    I'm not quite sure, but isn't `.submit()` refreshing/forwarding the page by default? submitting two forms at the same time might produce weird results anyway. if it wasn't refreshing the page though, adding a hidden element would cause weird results, when - after the first button press - a different button was pressed (since the action name is encoded in the field name instead of the field value, and adding another hidden element would cause both branches to run) – Jakumi Dec 10 '18 at 06:34
  • 1
    @Jakumi - I believe you are correct, but it's a different question/problem. If the OP wants to submit multiple forms they need to use ajax. The question I'm answering here is about why the submit button value doesn't come through. – But those new buttons though.. Dec 10 '18 at 06:46
  • @billynoah...tried as suggested...the if(isset($_POST['create_record'])){ condition is not failing now,however $_POST['create_record'] is coming out empty – andy Dec 10 '18 at 08:17
  • @andy - I've tested this and it works fine. If create_record is coming out empty for you, you're doing something other than what I posted here and I'd have to see more of your code to say what's causing it to fail. Also, please note, that nowhere in your question is that made a requirement. Although I included the value in my answer, it's more or less irrelevant to your question since you don't check the value anywhere in the code you presented. That said, here's a proof that it works: so-53699457.dev.zuma-design.com/index.php – But those new buttons though.. Dec 10 '18 at 13:06
0

Your code, isset($_POST['create_record']) maybe false or it didn't receive any values. If your query is only in one PHP file together with your jQuery, you need to check first your algorithm or use var_dump() for testing. Second, If it didn't work, make an alternative solution for it. Do the proper HTML code when using form or make another PHP file for receiving post purpose only.

<form action="directory_to_another_file" method="POST">
  <!-- SOME INPUTS HERE -->
  <input type="submit" value="Submit 1" name="create_record">
</form>

Try to test all of your codes.

  • Hi Jason, $_POST['create_record'] is not false because its working properly if the form is submitted without jQuery from the input itself...its returning false only when submitted through jQuery – andy Dec 10 '18 at 05:57
  • @andy Jason is actually correct. since forms allow multiple submit buttons, and the data of that button is only added to the POST data, when the button is pressed, the jquery version (not pressing any buttons) will not include the button's data. So, Jason is correct, although the answer could be improved. – Jakumi Dec 10 '18 at 06:14
  • Since it's an if-else condition, It will determine if its true or false. –  Dec 12 '18 at 23:28
-1

You have to set form method as "POST" type and if you want to receive the form data in same page then empty the "action" key otherwise give the target link.

    <?php 
        if(isset($_POST['create_record'])){
            print_r($_POST);
        }
    ?>
    
    <form action="" method="POST" id="form1">
        <input type="text" name="create_record" value="Submit 1"/>
    </form>
    <a href="#" id="myButton"> Submit </a>

    <script>
        $(function(){
            $("#myButton").click(function (event) {
                event.preventDefault();
                $("#form1").submit();
            });
        })
    </script>

Let me know if it's work for you.

Sahid Hossen
  • 1,377
  • 12
  • 14
  • since op said, the form works when not being submitted via jquery, it's safe to assume, the form already has the POST method. – Jakumi Dec 10 '18 at 06:09
  • Hi Sahid, tried that ,still no luck...reckon the $_POST['create_record'] is coming out empty whereas just $_POST is getting the data – andy Dec 10 '18 at 06:15