简体   繁体   English

如何在 php class 函数调用中使用 ajax 显示错误?

[英]how to display error using ajax in php class functions call?

Alright, this is probably super simple but I've been breaking my head over this all day and I cannot get it to work.好吧,这可能非常简单,但我整天都在为此烦恼,我无法让它工作。

I have a page on which there is a function that addusers.我有一个页面,上面有一个添加用户的 function。 To do this, I'm sending an AJAX call to handler.php which does some validation and sends an error if there is one, or success message if everything is ok.为此,我向 handler.php 发送了一个 AJAX 调用,它会进行一些验证并在有错误时发送错误,如果一切正常则发送成功消息。

In procedural PHP I can easily solve this problem by this approach:在程序 PHP 中,我可以通过以下方法轻松解决此问题:

NOTE:(My approach) This is just a demo code to show how ajax handle ERROR:SUCCESS messages注意:(我的方法)这只是一个演示代码,用于展示 ajax 如何处理 ERROR:SUCCESS 消息

<p id="display_error"></p> // all success/error display here 

Ajax success Ajax 成功

success: function(response) 
{
      if((response !== "") && ($.isNumeric(response))) {
        {
         //redirect in ajax success
         location.href = "http://localhost/manage/info.php?id="+ response;
        } 
      else {
             //this will display the custom error.
             $("#display_error").html("<p>" + response + "</p>"); //output: something went wrong!
           }
 }

PHP PHP

if(num_rows > 0) {
    $conn = mysqli_connect();
    $query = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('$fname', '$lname', '$email')";
    $result = mysqli_query($conn, $query) or die ('Error Could Not Query');

    $id = mysqli_insert_id($result);

    echo $id;

    mysqli_close($conn);
} else { 
    echo 'something went wrong!';
}

But now I switch to classes...I and dont know how to display an error or success message using ajax in php functions.但是现在我切换到类...我不知道如何在 php 函数中使用 ajax 显示错误或成功消息。 The problem is in function call.问题出在 function 调用中。 its a basics that PHP display the error or success message from where the actual function is called.它是 PHP 显示错误或成功消息的基础,其中实际的 function 被调用。 In the below code the function is called from index page which is out of reach from ajax to fetch the success or error messages.在下面的代码中,从 ajax 无法访问的索引页面调用 function 以获取成功或错误消息。 Is there any better approach in php ajax error handling? php ajax 错误处理中是否有更好的方法? I am new in this..love to see..我是新来的……喜欢看……

handler.php handler.php

function addUser($firstname, $lastname, $email)
{
    global $db;

   // prepare and bind
   $stmt = $db->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
   $stmt->bind_param("sss", $firstname, $lastname, $email);


   if($stmt->execute())
   {
      echo "New records created successfully";
   }else
   {
     echo "something went wrong!";
   }

   $stmt->close();


}

addUser('Navjot','singh','ns00@domain.com'); //calling from index.php page 

Use json_encode for the ajax call result.对 ajax 调用结果使用 json_encode。 For example, echo json_encode(array('status' => 1, 'your value' => ''));例如, echo json_encode(array('status' => 1, 'your value' => '')); or echo json_encode(array('status' => 0, 'error_msg' => ''));或 echo json_encode(array('status' => 0, 'error_msg' => ''));

And in your front-end, if you are using PHP then you can use json_decode(), if you are using javascript then you can use JSON.parse()在你的前端,如果你使用 PHP 那么你可以使用 json_decode(),如果你使用 javascript 那么你可以使用 JSON.parse()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM