Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
模态窗口中的jquery/php表单_Php_Jquery - Fatal编程技术网

模态窗口中的jquery/php表单

模态窗口中的jquery/php表单,php,jquery,Php,Jquery,我在模态窗口中有一个表单。当我通过ajax提交表单时,我没有收到成功消息。我的目标是在提交表单后在模式中查看php文件中创建的消息。代码如下: <p><a class='activate_modal' name='modal_window' href='#'>Sign Up</a></p> <div id='mask' class='close_modal'></div> <div id='modal_wind

我在模态窗口中有一个表单。当我通过ajax提交表单时,我没有收到成功消息。我的目标是在提交表单后在模式中查看php文件中创建的消息。代码如下:

<p><a class='activate_modal' name='modal_window' href='#'>Sign Up</a></p>   
<div id='mask' class='close_modal'></div>

<div id='modal_window' class='modal_window'>
    <form name="field" method="post" id="form">
        <label for="username">Username:</label><br>
        <input name="username" id="username" type="text"/><span id="gif"><span>   
        <span id="user_error"></span><br><br>
        <label for="email">Email:</label><br>
        <input name="email" id="email" type="text"/><span id="gif3"></span>    
        <span id="email_error"></span><br><br>
        <input name="submit" type="submit" value="Register" id="submit"/>
    </form>
</div>
test.js用于注册用户

$(function() {
    $('#form').submit(function() {
    $.ajax({
       type: "POST",
       url: "test.php",
       data: $("#form").serialize(),
           success: function(data) {
       $('#form').replaceWith(data);
       }
           });
    }); 
});
和PHP文件

<?php

$mysqli = new mysqli('127.0.0.1', 'root', '', 'project');

    $username = $_POST['username'];
    $email = $_POST['email'];

$mysqli->query("INSERT INTO `project`.`registration` (`username`,`email`) VALUES ('$username','$email')");

$result = $mysqli->affected_rows;

if($result > 0) {
    echo 'Welcome';
} else {
    echo 'ERROR!';
}

?>

尝试将AJAX调用中的返回代码放入

$('#modal_window')
而不是在形式上

$('#form')
顺便问一下:为什么不使用jQuery的POST或GET方法呢?它们非常容易使用…

试试这样的东西。 首先使用jquery编写ajax代码

<script type="text/javascript">

function submitForm()
{

    var str = jQuery( "form" ).serialize();
       jQuery.ajax({
        type: "POST", 
        url:  '<?php echo BaseUrl()."myurl/"; ?>',
        data:  str,
        format: "json",
        success: function(data) {

            var obj =  JSON.parse(data);

            if( obj[0] === 'error')
            {

                 jQuery("#error").html(obj[1]);

             }else{

                    jQuery("#success").html(obj[1]);
                     setTimeout(function () {
                     jQuery.fancybox.close();
                 }, 2500);
                    }
        } 

      });    
}

</script>

希望这对您有所帮助。

您的意思是:$.post('file.php',{variable\u name:variable},function(data){});
<script type="text/javascript">

function submitForm()
{

    var str = jQuery( "form" ).serialize();
       jQuery.ajax({
        type: "POST", 
        url:  '<?php echo BaseUrl()."myurl/"; ?>',
        data:  str,
        format: "json",
        success: function(data) {

            var obj =  JSON.parse(data);

            if( obj[0] === 'error')
            {

                 jQuery("#error").html(obj[1]);

             }else{

                    jQuery("#success").html(obj[1]);
                     setTimeout(function () {
                     jQuery.fancybox.close();
                 }, 2500);
                    }
        } 

      });    
}

</script>
  if(//condition true){
                    echo json_encode(array("success"," successfully Done.."));

                }else{
                    echo json_encode(array("error","Some  error.."));
                }