Calling PHP file from jQuery not working [closed]

2019-09-20 05:05发布

I am trying to execute an entire PHP file via the onClick event of some link.

<a onClick="phpSubmit();" class="submitButton">Submit</a>

Makes a successful call to this jQuery function

function phpSubmit()
{
    alert('submitting');

    $.post("/submit.php");

    alert('submitted'); 
}

Which fails to execute the PHP file "submit.php", in the same directory.

Any ideas?

I have tried $.ajax, $.get and "submit.php" as well as "/submit.php". I don't know why this isn't working, repost $.ajax stuff if you think I was doing something wrong.

EDIT:

It's always the little things, I mistyped a character when I imported the jQuery script which is why $.ajax was undefined.

The above method WORKS.

3条回答
我只想做你的唯一
2楼-- · 2019-09-20 05:20

use $.post("submit.php"); without a slash

查看更多
别忘想泡老子
3楼-- · 2019-09-20 05:21

Rather than use $.post I prefer, and find it much easier for debugging, to use something like:

$.ajax({
   dataType: 'html',
   type: 'POST',
   url: 'submit.php',
   cache: false,
   data: 'foo=bar',
   error: function(e){
      alert(e.status);
   },
   success: function(response){
      alert(response);
   }
});

I would also suggest removing the / in front of submit.php - no need if it's in the same directory.

查看更多
成全新的幸福
4楼-- · 2019-09-20 05:39

You can get the error message if fail to post the data to PHP file. use $.ajax();

          error : function(XMLHttpRequest,textStatus,ErrorThrown)
          {
                alert(textStatus);
          }
查看更多
登录 后发表回答