doudu9148 2015-12-08 23:42
浏览 18

使用AJAX / JSON进行PHP验证

My validation is in my php script encoded in json, I'm not sure how to implement it in my main JavaScript function. I'm using regular expressions to in my php script to validate the form criteria, I need to pass this to the JavaScript file and return a success massage for for each form id.
this is what I have so far.

$(document).ready(function()
{   
  $("#submit").click(function()
    {
        // Clear any success or error messages
        $("#success").html("");
        $("#errors").empty();
         //make an AJAX call here, and set error or success accordingly
        $.post('backend.php',{act:'validate'},
        function(getData)
        {
        //unsure about this function
        }
        });
        // prevents submit button default behavior
        return false;
    });  
});
<html>
    <head>
    </head>
    <body>
        <h1>Form Validation</h1>
        <form id="PersonForm">
            Name:
                <input id="name" type ="text" name="name"></input>
                <br><br>
            Postal Code:
                <input id="postal" type ="text" name="postal"></input>
                <br><br>
            Phone Number:
                <input id="phone" type ="text" name="phone"></input>
                <br><br>
            Address:
                <input id="address" type ="text" name="address"></input>
                <br><br>
            <input type="submit"></input>
        </form>
        <div id= "content"></div>
        <a href="frontend.html">Refresh</a>
        <a id="InsertDefault" href="#">Insert Default Data</a>
        <br><br>
        <p id='msg'></p>
        <ul id="errors"></ul>
        <p id="success"></p>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <script src="main.js" type="text/javascript"></script>
    </body>
</html>

<?php
if ($_REQUEST['act'] == 'validate')
{
  $validateData = array();
  if (preg_match("/^[A-Za-z]{3,20}$/",$_REQUEST['name'])) $validateData['name'] = 1;
  else $validateData['name'] = 0;

  if (preg_match("/^[0-9]{10}$/",$_REQUEST['phone'])) $validateData['phone'] = 1;
  else $validateData['phone'] = 0;

  if (preg_match("/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/", $_REQUEST['postal'])) $validateData['postal'] = 1;
  else $validateData['postal'] = 0;

  if (preg_match("/^[0-9]{3} [A-Za-z]{3,10} Street$/", $_REQUEST['address'])) $validateData['address'] = 1;
  else $validateData['address'] = 0;

  echo json_encode($validateData);
}
else echo "Should not happen";
?>
</div>
  • 写回答

2条回答 默认 最新

  • doushajian2018 2015-12-09 00:12
    关注

    Well, this would be better suited for an actual ajax call set up like this (Not Tested)

    // Set values
    var values = {};
    values.act = "validate";
    values.name = $('[name="name"]').val();
    values.phone = $('[name="phone"]').val();
    values.postal= $('[name="postal"]').val();
    values.address= $('[name="address"]').val();
    
    $.ajax({
        type: 'POST',
        url:'backend.php',
        data: values,
        dataType: 'JSON',
        success: 
        function(result)
        {
            // Here you can access the json object like this and
            // do whatever you like with it
            console.log(result.name);
            console.log(result.phone);
            console.log(result.postal);
            console.log(result.address);    
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致