我有一個窗體,將通過AJAX加載到另一個頁面上的div。點擊時,我想檢查內容的必填字段。如果必填字段沒有填充,我想在此字段旁邊放置一條消息,如下例所示:http://www.w3schools.com/php/php_form_required.asp。PHP窗體必填字段
如果名稱被要求舉例來說,在這裏我想補充
if (empty($_POST["name"]))
{$nameErr = "Name is required";}
else
{$name=$_POST['name'];}
在提交:
<html>
<body>
<?php
// Connect to DB
XXX
$phone=$_POST['phone'];
$category=$_POST['category'];
implode(',',$_POST['check']);
//or something like
$checkstring="";
foreach($_POST['check'] as $checkboxes) {
$checkstring.= $checkboxes .",";
}
$c = rtrim($checkstring);
$query = "INSERT INTO contact VALUES ('$name','$phone', '$c', '$category')";
mysql_query($query) or die('Error, insert query failed');
echo "Hi! $name. Your phone number <b> $phone </b> is in our database";
?>
</body>
</html>
在這裏,我想補充
<?php echo $nameErr;?>
旁邊名領域。 但是,當我在PHP變量把
?>
,是因爲它認爲我結束
<?php
在窗體頂部打破了變量。
形式:
<?php
// Get value of clicked button
$button = $_GET['button'];
$test = '<center>
<table>
<tr>
<td style="height: 20px;"></td>
</tr>
</table>
<h1>Demo</h1>
<form id="ContactForm" onsubmit="return submitForm();">
<div class="form_result"> </div>
Name: <input name="name" type=text ><br>
Phone: <input name="phone" type=text ><br>
Business Capabilities:<br>
<input type="checkbox" name="check[]" value="c1">C1<br>
<input type="checkbox" name="check[]" value="c2">C2<br>
<select name="category">Category:
<option value="d1">D1</option>
<option value="d2">D2</option>
<option value="d3">D3</option>
</select><br>
<input type="submit" >
</form>
</center>
';
print json_encode($test);
?>
是否有可能對我來說,把那PHP語句在PHP變量?或者還有另外一種更乾淨的方法嗎?謝謝你的幫助。
請使用'mysqli_ *'函數或'PDO'類來代替已棄用的'mysql_ *'。 – Sam