doukui9491 2016-09-15 18:21
浏览 31
已采纳

如何检查成功的数据在PHP文件中使用MySQL提交

I'm submitting a form using MySQL command inside a PHP file. I'm able to insert the data without any problem.

However, I also, at the same time, want to display the user a "Thank you message" on the same page so that he/she knows that the data has been successfully registered. On the other hand I could also display a sorry message in case of any error.

Therein lies my problem. I've written some lines in Javascript to display the message in the same page. However, I'm stuck on what (and how) should I check for success and failure.

I'm attaching my code below.

Can you please help me on this with your ideas?

Thanks AB

HTML Form tag:

<form id="info-form" method="POST" action="form-submit.php">

form-submit.php:

<?php   
    require("database-connect.php");

    $name = $_POST['name'];
    $email = $_POST['email'];
    $mobile = $_POST['mobile'];

    $sql = "INSERT INTO tbl_details ".
           "(name,email_id,mobile_number) ".
           "VALUES ".
           "('$name','$email','$mobile')";

    mysql_select_db('db_info');     
    $retval = mysql_query( $sql, $conn );

    if(! $retval )
    {
        die('Could not enter data: ' . mysql_error());
        return false;
    }

    echo "Entered data successfully
";
    mysql_close($conn);
?>

submit-logic.js:

$(function () 
{
    $('form').submit(function (e) 
    {                   
        e.preventDefault();

        if(e.target === document.getElementById("info-form"))
        {
            $.ajax(
            {
                type:this.method,
                url:this.action,
                data: $('#info-form').serialize(),
                dataType: 'json',    
                success: function(response)
                {
                    console.log(response);                          
                    if(response.result == 'true') 
                    {
                        document.getElementById("thankyou_info").style.display = "inline";
                        $('#please_wait_info').hide();
                        document.getElementById("info-form").reset();
                    }                       
                    else 
                    {           
                        document.getElementById("thankyou_info").style.display = "none";
                        document.getElementById("sorry_info").style.display = "inline";
                        $('#please_wait_info').hide();  
                    }
                }
            }
        )}; 
    });
}
  • 写回答

1条回答 默认 最新

  • dongyue9860 2016-09-15 18:28
    关注

    Per documentation: http://api.jquery.com/jquery.ajax/

    dataType (default: Intelligent Guess (xml, json, script, or html))
    Type: String
    The type of data that you're expecting back from the server.
    

    You are explicitly setting this to json but then returning a string. You should be returning json like you are telling the ajax script to expect.

    <?php   
        require("database-connect.php");
    
        $name = mysql_real_escape_string($_POST['name']);
        $email = mysql_real_escape_string($_POST['email']);
        $mobile = mysql_real_escape_string($_POST['mobile']);
    
        $sql = "INSERT INTO tbl_details ".
               "(name,email_id,mobile_number) ".
               "VALUES ".
               "('$name','$email','$mobile')";
    
        mysql_select_db('db_info');     
        $retval = mysql_query( $sql, $conn );
    
        if(! $retval )
        {
            die(json_encode(array('result' => false, 'message' => 'Could not enter data: ' . mysql_error()));
        }
    
        echo json_encode(array('result' => true, 'message' => 'Entered data successfully'));
        mysql_close($conn);
    ?>
    

    I also added code to sanitize your strings, although mysql_* is deprecated and it would be better to upgrade to mysqli or PDO. Without sanitization, users can hack your database..

    Nevertheless, returning json properly will ensure that your response in success: function(response) is an object, and response.result will be returned as expected, and you can use response.message to display the message where you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集