Dylan
Dylan

Reputation: 137

PHP Post is not setting value on jquery form submit

PHP Form

<form id="f1" action="welcome.php" method="post">
  Name: <input type="text" name="name"><br>
  E-mail: <input type="text" name="email"><br>
  <input name="btnSubmit" id="btnSubmit" type="submit">
</form>

PHP Post

if($_SERVER["REQUEST_METHOD"] == "POST")
{
  if (isset($_POST['btnSubmit'])) {
      echo 'You have clicked submit button';
  }
}

Here the value is printing. But when we submit the form using the jquery:

 $('#btnSubmit').on('click',function(e){
    e.preventDefault();
    var form = $(this).parents('form');
    swal({
        title: "Are you sure?",
        text: "You will not be able to recover this imaginary file!",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, delete it!",
        closeOnConfirm: false
    }, function(isConfirm){
        if (isConfirm) form.submit();
    });
});

The echo value is not printing. How can I print the echo value using jquery form submit?

SweetAlert: https://sweetalert.js.org/guides/

Upvotes: 0

Views: 165

Answers (2)

jbrahy
jbrahy

Reputation: 4425

Try this;

if($_SERVER["REQUEST_METHOD"] == "POST")
{
    print_r($_POST); 
}

To see if you get value for btnSubmit. Pretty sure you need to attach value for it to set a key/value pair in the $_POST.

The easiest way to make this work is to change your button to this:

<input name="btnSubmit" id="btnSubmit" type="submit" value="Submit">

The security issues presented when you don't check input are huge. Just check out Johnny Tables (https://xkcd.com/327/)

Upvotes: 0

Julien Baldy
Julien Baldy

Reputation: 426

btnSubmit is not on your $_POST variable; only type text (in your case) is.

Upvotes: 0

Related Questions