PHP表单提交,验证变量 [英] PHP form submission, validate variables
问题描述
这是我迄今为止的内容(有些内容目前可能丢失我一直在调整它,但你会明白)
index.php
< div id =tabs-2>
< center>< h1>添加活动表单:< / h2>< / center>
< form name =htmlformmethod =POSTaction =htmlform.php>
< table>
< tr>
< td class =left>< label for =first_name>事件名称< / label>< / td>
< td>< span>< input type =textname =event_namevalue =<?PHP if(isset($ _ POST ['event_name']))echo htmlspecialchars($ _ POST ['event_name']);?> required />< / span>< / td>
< / tr>
< tr>
< td class =left>< label for =first_name> Event Date *< / label>< / td>
< td>< span>< input id =datepickertype =textname =event_datevalue =<?PHP if(isset($ _ POST ['event_date'])) echo htmlspecialchars($ _ POST ['event_date']);?> required />< / span>< / td>
< / tr>
< tr>
< td class =left>< label for =first_name>开始时间*< / label>< / td>
< td>
< span>
< select name =start_timevalue =<?PHP if(isset($ _ POST ['start_time']))echo($ _POST ['start_time']);?>必需/>
<?php
foreach($ time as $ time){
echo'< option value =''。$ time。'>'。 $时间。 < /选项>;
}
?>
< / span>
< / td>
< / tr>
< tr>
< td class =left>< label for =first_name>结束时间*< / label>< / td>
< td>
< span>
<?php
foreach($ time as $ time){
echo'< option value =''。$ time。'>'。 $时间。 < /选项>;
}
?>
< / span>
< / td>
< / tr>
< tr>
< td class =left>< label for =first_name> Venue Name *< / label>< / td>
< td>< span>< input type =textname =venue_namevalue =<?PHP if(isset($ _ POST ['venue_name']))echo htmlspecialchars($ _ POST ['venue_name']);?> required />< / span>< / td>
< / tr>
< tr>
< td class =left>< label for =first_name> Venue Address *< / label>< / td>
< td>< span>< input type =textname =venue_addressvalue =<?PHP if(isset($ _ POST ['venue_address']))echo htmlspecialchars($ _ POST ['venue_address']);?> required />< / span>< / td>
< / tr>
< tr>
< td class =left>< label for =first_name>描述< / label>< / td>
< td>< span>< textarea name =descriptionstyle =height:150px; font-size:13px; value =<?PHP if(isset($ _ POST ['description']))echo htmlspecialchars($ _ POST ['description']);?>必需>< / textarea>< / span>< / td>
< / tr>
< tr>
< td class =left>< label for =first_name>事件网址*< / label>< / td>
< td>< span>< input type =textname =event_urlvalue =<?PHP if(isset($ _ POST ['event_url']))echo htmlspecialchars($ _ POST ['event_url']);?> required />< / span>< / td>
< / tr>
< tr>
< td class =left>< label for =first_name>事件图片网址*< / label>< / td>
< td>< span>< input type =textname =event_imagevalue =<?PHP if(isset($ _ POST ['event_image']))echo htmlspecialchars($ _ POST ['event_image']);?> required />< / span>< / td>
< / tr>
< / table>
< p>< input class =eventSubtype =submitname =form_submitvalue =Submit>< / p>
< br />
< / form>
< / div>
和 htmlform.php
<?php
$ event_name = $ _POST ['event_name'];
$ event_date = $ _POST ['event_date'];
$ start_time = $ _POST ['start_time'];
$ end_time = $ _POST ['end_time'];
$ venue_name = $ _POST ['venue_name'];
$ venue_address = $ _POST ['venue_address'];
$ description = $ _POST ['description'];
$ event_url = filter_var($ _ POST ['event_url'],FILTER_SANITIZE_URL);
$ event_image = filter_var($ _ POST ['event_image'],FILTER_SANITIZE_URL);
$ start = strtotime($ start_time);
$ end = strtotime($ end_time);
if(filter_var($ event_url,FILTER_VALIDATE_URL)=== false){
//die(header(\"location:index.php?error=true&reason=url#tabs- 2\" )); ($ event_image,FILTER_VALIDATE_URL)=== false){
//die(header = URL#突片-2\" ));
if($ end< $ start){
$ next_day_format = strtotime(+ 1 day,strtotime($ event_date));
$ next_day = date(Y-m-d,$ next_day_format);
$ json_start_time = $ event_date。 T。 $ START_TIME;
$ json_end_time = $ next_day。 T。 $ END_TIME;
}
其他{
$ json_start_time = $ event_date。 T。 $ START_TIME;
$ json_end_time = $ event_date。 T。 $ END_TIME;
}
$ cityclean = str_replace(,+,$ venue_address);
$ fullurl =http://maps.googleapis.com/maps/api/geocode/json?address=。 $ cityclean。 &安培;传感器=真;
$ string = file_get_contents($ fullurl);
$ params = json_decode($ string,true);
$ lon = $ params ['results'] [0] ['geometry'] ['location'] ['lng'];
$ address = substr($ params ['results'] [0] ['formatted_address'],0,-5);
$ lat = $ params ['results'] [0] ['geometry'] ['location'] ['lat'];
$ address_array = explode(',',$ address);
$ state_zip = trim($ address_array [2]);
$ sz_array = explode('',$ state_zip);
$ state = $ sz_array [0];
$ city = trim($ address_array [1]);
$ zip = $ sz_array [1];
?>
尚未完成。理想情况下,一旦所有字段都被验证,将提交发送给json格式的字符串。
问题是如果有任何验证失败,我想重新加载上一页表单已提交但保留所有输入的值,截至目前,当它出错并重新加载页面时,表单为空。 解决方案
你能做到的最好的方法就是使用Ajax。
<1>。点击提交按钮后,通过Ajax将所有值提交给php文件,并从php文件中获取结果。
2)。如果php生成错误,则显示所需的错误。
<3>。如果php没有产生任何错误,触发
表单,它将被提交。
以下是您的一些快速代码。
< script>
$(#form_submit)。click(function(e){
e.preventDefault();
$ .ajax({
type:POST,
url:yourBackendFile.phps
data:$('#form')。serialize(); //在表单中添加一个id属性到id =form
}) .done(函数(msg){
if(msg == 1)
{
//如果一切正常,则从php脚本回显1)
$(#form ).trigger(submit); //现在提交表单
}
else
{
//在某处显示'msg',现在这个msg将包含所有的php生成的错误。
}
});
});
< / script>
I am hung up on an issue. I've got an html form, nothing fancy. On submission I need to verify the values submitted and if they fail I need to go back to the form and point out the error. I've done this many times before but with simple data verification like blank input fields or email address formatting. There is a lot of validation here and when I return to my form all the previous input it lost.
Here's what I have so far (some things may be currently missing as I am constantly tweaking it but you'll get the idea)
index.php
<div id="tabs-2">
<center><h1>Add Event Form:</h2></center>
<form name="htmlform" method="POST" action="htmlform.php">
<table>
<tr>
<td class="left"><label for="first_name">Event Name *</label></td>
<td><span><input type="text" name="event_name" value="<?PHP if(isset($_POST['event_name'])) echo htmlspecialchars($_POST['event_name']); ?>" required /></span></td>
</tr>
<tr>
<td class="left"><label for="first_name">Event Date *</label></td>
<td><span><input id="datepicker" type="text" name="event_date" value="<?PHP if(isset($_POST['event_date'])) echo htmlspecialchars($_POST['event_date']); ?>" required /></span></td>
</tr>
<tr>
<td class="left"><label for="first_name">Start Time *</label></td>
<td>
<span>
<select name="start_time" value="<?PHP if(isset($_POST['start_time'])) echo ($_POST['start_time']); ?>" required />
<?php
foreach($times as $time){
echo '<option value="' . $time . '">' . $time . '</option>';
}
?>
</span>
</td>
</tr>
<tr>
<td class="left"><label for="first_name" >End Time *</label></td>
<td>
<span>
<select name="end_time" value="<?PHP if(isset($_POST['end_time'])) echo ($_POST['end_time']); ?>" required />
<?php
foreach($times as $time){
echo '<option value="' . $time . '">' . $time . '</option>';
}
?>
</span>
</td>
</tr>
<tr>
<td class="left"><label for="first_name">Venue Name *</label></td>
<td><span><input type="text" name="venue_name" value="<?PHP if(isset($_POST['venue_name'])) echo htmlspecialchars($_POST['venue_name']); ?>" required /></span></td>
</tr>
<tr>
<td class="left"><label for="first_name">Venue Address *</label></td>
<td><span><input type="text" name="venue_address" value="<?PHP if(isset($_POST['venue_address'])) echo htmlspecialchars($_POST['venue_address']); ?>" required /></span></td>
</tr>
<tr>
<td class="left"><label for="first_name">Description</label></td>
<td><span><textarea name="description" style="height: 150px; font-size: 13px;" value="<?PHP if(isset($_POST['description'])) echo htmlspecialchars($_POST['description']); ?>" required ></textarea></span></td>
</tr>
<tr>
<td class="left"><label for="first_name">Event URL *</label></td>
<td><span><input type="text" name="event_url" value="<?PHP if(isset($_POST['event_url'])) echo htmlspecialchars($_POST['event_url']); ?>" required /></span></td>
</tr>
<tr>
<td class="left"><label for="first_name">Event Image URL *</label></td>
<td><span><input type="text" name="event_image" value="<?PHP if(isset($_POST['event_image'])) echo htmlspecialchars($_POST['event_image']); ?>" required /></span></td>
</tr>
</table>
<p><input class="eventSub" type="submit" name="form_submit" value="Submit"></p>
<br/>
</form>
</div>
and htmlform.php
<?php
$event_name = $_POST['event_name'];
$event_date = $_POST['event_date'];
$start_time = $_POST['start_time'];
$end_time = $_POST['end_time'];
$venue_name = $_POST['venue_name'];
$venue_address = $_POST['venue_address'];
$description = $_POST['description'];
$event_url = filter_var($_POST['event_url'], FILTER_SANITIZE_URL);
$event_image = filter_var($_POST['event_image'], FILTER_SANITIZE_URL);
$start = strtotime($start_time);
$end = strtotime($end_time);
if (filter_var($event_url, FILTER_VALIDATE_URL) === false) {
//die(header("location:index.php?error=true&reason=url#tabs-2"));
}
if (filter_var($event_image, FILTER_VALIDATE_URL) === false) {
//die(header("location:index.php?error=true&reason=url#tabs-2"));
}
if ($end < $start){
$next_day_format = strtotime("+1 day", strtotime($event_date));
$next_day = date("Y-m-d", $next_day_format);
$json_start_time = $event_date . "T" . $start_time;
$json_end_time = $next_day . "T" . $end_time;
}
else {
$json_start_time = $event_date . "T" . $start_time;
$json_end_time = $event_date . "T" . $end_time;
}
$cityclean = str_replace (" ", "+", $venue_address);
$fullurl = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $cityclean . "&sensor=true";
$string = file_get_contents($fullurl);
$params = json_decode($string, true);
$lon = $params['results'][0]['geometry']['location']['lng'];
$address = substr($params['results'][0]['formatted_address'],0,-5);
$lat = $params['results'][0]['geometry']['location']['lat'];
$address_array = explode(',',$address);
$state_zip = trim($address_array[2]);
$sz_array = explode(' ',$state_zip);
$state = $sz_array[0];
$city = trim($address_array[1]);
$zip = $sz_array[1];
?>
It's not quite finished yet. The ideal situation will send the submit to a json formated string once all fields are validated.
The question is if anything fails validation I would like to reload the previous page which the form was submitted but retain all entered values, as of now when it errors out and reloads the page, the form is blank.
The best way you could could get this done is to use Ajax.
1). As soon as the submit button is clicked, submit all values via Ajax to the php file, and get the result from the php file.
2). If php generated errors, then show the required error.
3). If php didn't generate any error, trigger
the form and it will be submitted.
Here's some quick code for you.
<script>
$("#form_submit").click(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "yourBackendFile.phps"
data: $('#form').serialize(); //add an 'id' attribute to your form with id = "form"
}).done(function( msg ) {
if(msg == 1)
{
//echo 1 from the php script if everything's fine.
$("#form").trigger("submit"); //submit the form now
}
else
{
//show the 'msg' somewhere. Now this msg would contain all the errors that the php generated.
}
});
});
</script>
这篇关于PHP表单提交,验证变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!