Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.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
验证提交的表单,但不将PHP重定向到操作页面_Php_Ajax_Jquery - Fatal编程技术网

验证提交的表单,但不将PHP重定向到操作页面

验证提交的表单,但不将PHP重定向到操作页面,php,ajax,jquery,Php,Ajax,Jquery,感谢AJAX,我想提交一个表单,并在提交时显示“form_treatment.php”(表单验证)中包含的错误消息,而不需要php重定向到“form_treatment.php”。我必须在下面的对话中写些什么 $.post("form_treatment.php",name, ???); 我在form.php中有一个基本表单: <form method="post" action="form_treatment.php" > <input type="text" n

感谢AJAX,我想提交一个表单,并在提交时显示“form_treatment.php”(表单验证)中包含的错误消息,而不需要php重定向到“form_treatment.php”。我必须在下面的对话中写些什么

$.post("form_treatment.php",name, ???); 
我在form.php中有一个基本表单:

<form method="post" action="form_treatment.php" >
    <input type="text" name="user_name" value="Your name..." /> 
    <button type="submit" >OK</button>
</form> 

您可以尝试以下方法:

<form id="myForm" method="post" action="form_treatment.php" >
    <span id="message"></span>
    <input type="text" name="user_name" value="Your name..." /> 
    <input type="submit" >OK</button>
</form>

您可以尝试以下方法:

<form id="myForm" method="post" action="form_treatment.php" >
    <span id="message"></span>
    <input type="text" name="user_name" value="Your name..." /> 
    <input type="submit" >OK</button>
</form>

您可以通过两种方式来实现这一点 同一页中的第一个 第二个是通过AJAX 第一种方法可以这样做

<?php
if(isset($_POST['sub']) && $_POST['sub'] == 'true'):
if ( empty($_POST['user_name']) ){      
    echo 'You have to enter your name.';        
} else { 
    $already_existing = verify_existence( $_POST['user_name'] ); 
    // verification in the DB, return true or false

    if( $already_existing ){
        echo 'Name already used.';
    } else {    
        // Entering the name in the DB
    }
}

endif; 
?>
<form method="post" action="<?php echo $PHP_SELF; ?>" >
    <input type="text" name="user_name" value="Your name..." /> 
    <input type="hidden" name="sub" value = "true" >
    <button type="submit" >OK</button>
</form> 


您可以通过两种方式来实现这一点 同一页中的第一个 第二个是通过AJAX 第一种方法可以这样做

<?php
if(isset($_POST['sub']) && $_POST['sub'] == 'true'):
if ( empty($_POST['user_name']) ){      
    echo 'You have to enter your name.';        
} else { 
    $already_existing = verify_existence( $_POST['user_name'] ); 
    // verification in the DB, return true or false

    if( $already_existing ){
        echo 'Name already used.';
    } else {    
        // Entering the name in the DB
    }
}

endif; 
?>
<form method="post" action="<?php echo $PHP_SELF; ?>" >
    <input type="text" name="user_name" value="Your name..." /> 
    <input type="hidden" name="sub" value = "true" >
    <button type="submit" >OK</button>
</form> 


当我提交时,它会在中显示整个页面html(带有页眉、页脚…)。问题是什么?请确保处理post提交的PHP脚本只生成您希望在span中显示的文本,而不生成页眉和页脚。事实上,这是因为我使用MVC,所以它会生成所有html页面代码。但请告诉我,如何获得(仍在我的ajax脚本中)由“form_treatment.php”返回的var?只有当“form_treatment.php”在提交时返回类似“Verification is OK”=)的字符串时,我才想关闭包含我的表单的colorbox(一个lightbox),它将在中显示整个页面html(带有页眉、页脚…)。问题是什么?请确保处理post提交的PHP脚本只生成您希望在span中显示的文本,而不生成页眉和页脚。事实上,这是因为我使用MVC,所以它会生成所有html页面代码。但请告诉我,如何获得(仍在我的ajax脚本中)由“form_treatment.php”返回的var?仅当“form_treatment.php”返回“Verification is OK”之类的字符串时,我才想关闭包含我的表单的colorbox(一个lightbox)。您好,您的代码可以运行,但当我提交时,它会在中显示整个页面html(带有页眉、页脚…)。有什么问题吗?您好,您的代码运行正常,但当我提交时,它会在中显示整个页面html(带有页眉、页脚…)。有什么问题吗?
<div id="errors"></div>
<form method="post" id="myFrm">
        <input type="text" name="user_name" value="Your name..." id="name" /> 
        <button type="submit" >OK</button>
</form> 

<sctipt>
$("#myFrm").submit(function() {
  var NameFromForm = $('#name').val(); 
   $.post("form_treatment.php", { "user_name": NameFromForm },
 function(data){
   $("#errors").html(data); 
 });
    event.preventDefault();
});
</script>