doupao6698 2018-05-17 22:28
浏览 73

数据未插入数据库 - PHP MYSQL

Trying to get together the sign up validation with PHP and Ajax. Not sure what is wrong but the submission does not happen. If I don't add the validation part everything looks fine and i am able to insert the data to mysql.

<script type="application/javascript">

    $("#submit").submit(function (event) {
        event.preventDefault();
        var datatopost = $(this).serializeArray();
        console.log(datatopost);
        $.ajax({
            url: "signupregister.php",
            type: "POST",
            data: datatopost,
            success: function (data) {
                if (data) {
                    $("#signupmessage".html(data));
                }
            },
            error: function () {
                $("#signupmessage").html("<div class = 'alert alert-danger'></div>")
            }
        });
    });
</script>

_

<?php    

session_start();
include('mysqlconnection.php');
include('index.php');

function customError($errors, $errorslevel)
{
}

set_error_handler("customError", E_ALL);

if (isset($_POST['submit'])) {
    if ($_POST($first_name) == "") {
        $errors .= $first_nameError;
    } else {
        $first_name =
            filter_var($_POST["first_name"], FILTER_SANITIZE_STRING);
    }
}


if ($errors) {
    $resultMessage = '<div class="alert alert-danger">' .
        $errors . '</div>';
    echo $resultMessage;
    exit;
}
$first_nameError = '<p>first name required</p>';
  • 写回答

1条回答 默认 最新

  • dongpu9481 2018-05-18 06:26
    关注

    First up, in your validation PHP script, you won't need to include 'index.php'

    Try redirecting the form to an empty page where you only include your validation data, while setting a session variable for the first error encountered.

    At the end of your validation, if your error variable contains an error, you can redirect to the form and display your said error at a convenient location. Keep in mind you will have to save form data in session variables if you want to preserve all user input (thus removing the hassle of refilling the form over and over again). If it doesn't, you can proceed to insert the data in your db then redirect to your desired landing page.

    Here's a sample code based on your input:

    <?php
      session_start();
      if(isset($_POST['submit'])){
            $_SESSION['fname'] = $_POST['fname'];
            //so on for your other variables
            if($_SESSION['fname'] == ""){
                $_SESSION['err'] = "First Name Required";
            }
            if(//insert your format validation for first name){
                 $_SESSION['err'] = "First Name Invalid";}
            }
            //end of validation
            if isset($_SESSION['err']){
               header('Location: myform.php');
            }
            else{
                //save all your variables into normal ones, i.e $fname-$_POST['fname'];
               //insert into database;
               //check correct insertion;
               //redirect to landing page;
    }
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序