gpt4 book ai didi

php - jQuery 发布和获取表单数据

转载 作者:行者123 更新时间:2023-11-28 20:52:08 24 4
gpt4 key购买 nike

提交表单后,我可以使用 $_POST 获取其字段值。但是,我正在尝试使用基本的 jQuery(没有任何插件)来检查是否有任何字段为空白,我只想在没有任何空白字段的情况下发布表单内容。

我正在尝试下面的代码,并且我使用 jQuery 取得了成功,但唯一的问题是我在使用 jQuery 检查后无法发布表单。它不会在 jQuery 之后到达 $_POST。

此外,如何在 jQuery 中获取服务器响应(以检查是否存在任何服务器错误)。这是我正在尝试的:

HTML:

<form action="" id="basicform" method="post">
<p><label>Name</label><input type="text" name="name" /></p>
<p><label>Email</label><input type="text" name="email" /></p>

<input type="submit" name="submit" value="Submit"/>
</form>

jQuery:

jQuery('form#basicform').submit(function() {
//code
var hasError = false;

if(!hasError) {
var formInput = jQuery(this).serialize();
jQuery.post(jQuery(this).attr('action'),formInput, function(data){

//this does not post data
jQuery('form#basicform').slideUp("fast", function() {
//how to check if there was no server error.
});
});
}
return false;


});

PHP:

if(isset($_POST['submit'])){
$name = trim($_POST['name'];
$email = trim($_POST['email'];

//no any error
return true;
}

最佳答案

要非常具体地回答这个问题:

How can I get the server response back in the jQuery (to check if there was any server error or not). Here's what I'm trying:

听起来就像您在谈论通过 jQuery-Ajax 进行服务器端验证。

那么你需要:

  • 将变量的 JavaScript 值发送到 PHP
  • 检查是否有错误发生
  • 将结果发送回 JavaScript

所以你是说,

However, I am trying to use a basic jQuery (without any plugin) to check if any field was blank, I want to post the form content only if there's no any blank field.

JavaScript/jQuery 代码:

看一下这个例子:

<script>
$(function()) {
$("#submit").click(function() {
$.post('your_php_script.php', {
//JS Var //These are is going to be pushed into $_POST
"name" : $("#your_name_field").val(),
"email" : $("#your_email_f").val()
}, function(respond) {
try {
//If this falls, then respond isn't JSON
var result = JSON.parse(respond);

if ( result.success ) { alert('No errors. OK') }
} catch(e) {
//So we got simple plain text (or even HTML) from server
//This will be your error "message"
$("#some_div").html(respond);
}
});
});
}
</script>

好吧,现在不是看 php one 的时候了:

<?php

/**
* Since you're talking about error handling
* we would keep error messages in some array
*/

$errors = array();

function add_error($msg){
//@another readers
//s, please don't tell me that "global" keyword makes code hard to maintain
global $errors;
array_push($errors, $msg);
}

/**
* Show errors if we have any
*
*/
function show_errs(){
global $errors;
if ( !empty($errors) ){

foreach($errors as $error){
print "<p><li>{$error}</li></p>";
}
//indicate that we do have some errors:
return false;
}
//indicate somehow that we don't have errors
return true;
}



function validate_name($name){
if ( empty($name) ){
add_error('Name is empty');
}

//And so on... you can also check the length, regex and so on
return true;
}


//Now we are going to send back to JavaScript via JSON

if ( show_errs() ){
//means no error occured

$respond = array();
$respond['success'] = true;
//Now it's going to evaluate as valid JSON object in javaScript
die( json_encode($respond) );

} //otherwise some errors will be displayed (as html)

关于php - jQuery 发布和获取表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12246412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com