drtppp75155 2014-12-02 13:45
浏览 62
已采纳

jQuery Ajax从php返回几个值

I'm picking errors from php file using ajax and i face few troubles. Like in php file i take errors into $email_error and $password_error so i want to return error reports to ajax and assign $email_error to id = "email_errors" and $password_error to id = "password_errors". Maybe someone could explain how i specify what variables i want to return and what id should it take .I will leave some commented code below. Thanks!

php

<?php


if (isset($_POST['email']) && isset($_POST['password1']) && isset($_POST['password2'])) {

$email = trim ($_POST['email']);
$password1 = trim ($_POST['password1']);
$password2 = trim ($_POST['password2']);

}

$email_error = 'No errors<br>';
$password_error = 'No errors<br>';

if (empty($email))
$email_error = ('Pleas fill in email field<br>');

if ($email == 'example')
$email_error =('There already is user with this email<br>');

if (empty($password1))
$password_error =  ('Please fill in password fields<br>'); 

if (empty($password2))
$password_error = ('Please fill in password fields<br>');

$email_error; //this variable must be returned to ajax and assigned to id "email_errors"
$password_error; //this variable must be returned to ajax and assigned to id "password_errors"

?>

javascript

$(document).ready(function ()   {

$('#push_button').click(function() {

$.post('php.php',
{
email : $('#email').val(), // i take these variables to php
password1 : $('#password1').val(),
password1 : $('#password2').val()
} ,
function ( data ) { //what do i return here?

$('#email_errors').val(data); //how to assign $emaill_error to #email_errors
$('#password_errors').val(data); //how to assign $password_error to #password_errors

}
)
})


})
  • 写回答

3条回答 默认 最新

  • dtol41388 2014-12-02 13:56
    关注

    If you want to return several variables to ajax, you would have to return some json

    PHP :

    // .. your php code
    $ret = array("email_error" => $email_error, "password_error" => $password_error);
    echo json_encode($ret);
    

    Be careful, json_encode needs PHP >= 5.2

    JS :

    $.ajax({
      url: "php.php",
      type: "POST",
      dataType: "json", // The type of data that you're expecting back from the server
      data: {
        email: $("#email").val(),
        password1: $("#password1").val(),
        password2: $("#password2").val() // careful, you had "password1" as variable name 2 times
      },
      success: function(obj) {
        // obj is your php array, transformed into a js object
        // you may want to use .html() instead of .val() since your errors are strings with html tags - depends if #email_errors / #password_errors are inputs or divs
        $("#email_errors").html(obj.email_error);
        $("#password_errors").html(obj.password_error);
      }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制