How does PHP handle AJAX-submitted forms?

Source: Internet
Author: User
Made a project of his own,
Using jquery's Ajax module to send data to a PHP page,
PHP page uses $_post['] to receive data,
Use echo json_encode ($respond) when returning;
However, it is not normal to print the returned JSON data in the AJAX success function.
And jump is not normal, Ajax is not jump page, right, after returning the results, the page jumps once to the page (refresh).

Did I make a mistake in which link?
Online and so on.

Jquery:

$(function(){    //登陆    $("#button_signip").click(function(){    });    //注册    $("#button_signup").click(function(){        console.log($("#logup_inputUser").value);        $.ajax({            type: 'post',            ulr: '../service/logupBusiness.php',            async: false,            data: $("#form_signup").serialize(),            beforeSend: function () {            },            complete: function () {            },            error: function(XMLHttpRequest, textStatus, errorThrown) {                alert(XMLHttpRequest.status);                alert(XMLHttpRequest.readyState);                alert(textStatus);            },            success: function (data, textStatus) {                console.log(data);//返回的结果不正常,并不是json                console.log(textStatus);                alert(data);            }        });    });});

PHP business logic (logupbusiness.php):


  
   register(array("username"=>$name, "password"=>$password, "email"=>$email));    $res = $bmobUser->login($name,$password);    echo json_encode($res);//返回结果,son}catch (Exception $e){    echo $e;}

Reply content:

Made a project of his own,
Using jquery's Ajax module to send data to a PHP page,
PHP page uses $_post['] to receive data,
Use echo json_encode ($respond) when returning;
However, it is not normal to print the returned JSON data in the AJAX success function.
And jump is not normal, Ajax is not jump page, right, after returning the results, the page jumps once to the page (refresh).

Did I make a mistake in which link?
Online and so on.

Jquery:

$(function(){    //登陆    $("#button_signip").click(function(){    });    //注册    $("#button_signup").click(function(){        console.log($("#logup_inputUser").value);        $.ajax({            type: 'post',            ulr: '../service/logupBusiness.php',            async: false,            data: $("#form_signup").serialize(),            beforeSend: function () {            },            complete: function () {            },            error: function(XMLHttpRequest, textStatus, errorThrown) {                alert(XMLHttpRequest.status);                alert(XMLHttpRequest.readyState);                alert(textStatus);            },            success: function (data, textStatus) {                console.log(data);//返回的结果不正常,并不是json                console.log(textStatus);                alert(data);            }        });    });});

PHP business logic (logupbusiness.php):


  
   register(array("username"=>$name, "password"=>$password, "email"=>$email));    $res = $bmobUser->login($name,$password);    echo json_encode($res);//返回结果,son}catch (Exception $e){    echo $e;}

    1. Provide your success function.

    2. Provide the results of your backend in use echo json_encode($respond); .

Follow up with your updated content:

    1. Provides the button_signup HTML code for this DOM node with ID.

    2. Provide the content of the logupBusiness.php backend output when requesting this PHP file, instead of providing you with this PHP source code.

However, in your current code, guess your button_signup counterpart may be a type=submit type of button, and then after the point, submitted the form, and then jumped away.

That must be something wrong, but from the information you provide you can't see where there is a problem. You can notice whether the json_encode result is normal, and if JS has other methods to deal with the result.

$.ajax
Success:function (callback) {alert (callback)},

Php:
Echo Json_encode ($str, json_force_object);

Do not post code out of everyone can not help you, if more privacy, you may paste a demo code is also OK, we help you to analyze where the wrong.

Landlord better put the code out

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.