[英]WordPress: Empty row is added on page (load / refresh)
I'm struggling with data insertion in the database. 我正在努力将数据插入数据库中。 I have created a form and the values are stored in a table. 我创建了一个表单,并将值存储在表中。 When i reload or refresh the page an empty row or the last stored values are stored in the db. 当我重新加载或刷新页面时,空行或最后存储的值存储在数据库中。 I don't know how to resolve it. 我不知道该怎么解决。 I really need your help. 我真的需要你的帮助。 This is my code 这是我的代码
<?php get_header();?>
<script type="text/javascript">
<!--
// Form validation code will come here.
function validate()
{
if( document.myForm.Name.value == "" )
{
alert( "Please provide your name!" );
document.myForm.Name.focus() ;
return false;
}
if(document.myForm.percentage.value =="")
{
alert("please fill the percentage value");
document.myForm.percentage.focus();
return false;
}
if( document.myForm.EMail.value == "" )
{
alert( "Please provide your Email!" );
document.myForm.EMail.focus() ;
return false;
}
if( document.myForm.Phone.value == "" ||
isNaN( document.myForm.Phone.value ) ||
document.myForm.Phone.value.length != 10 )
{
alert( "Please provide a valid phone number" );
document.myForm.Phone.focus() ;
return false;
}
if( document.myForm.Country.value == "-1" )
{
alert( "Please provide your country!" );
return false;
}
return true;
}
//-->
</script>
<form name="myForm" method="post" onsubmit="return(validate());">
<div class="row" id="enquirynew">
<div class="col-md-2" >name<span style="color:red;font-size:20px;">*</span></div>
<div class="col-md-4"><input type="text" name="Name" /></div>
<div class="col-md-2" >email<span style="color:red;font-size:20px;">*</span></div>
<div class="col-md-4"><input type="text" name="EMail" /></div>
</div>
<div class="row" id="enquirynew">
<div class="col-md-2" >Phone<span style="color:red;font-size:20px;">*</span></div>
<div class="col-md-4"><input type="text" name="Phone" /></div>
<div class="col-md-2" >Date of birth</div>
<div class="col-md-4">
<input type="text" name="dateofbirth" />
</div>
</div>
<div class="row" id="enquirynew">
<div class="col-md-2" >College</div>
<div class="col-md-4">
</div>
</div>
<div class="row" id="enquirynew">
<div class="col-md-2">Year of Passed out</div>
<div class="col-md-4"><input type="text" name="yearofpassedout" /></div>
<div class="col-md-2">Percentage<span style="color:red;font-size:20px;">*</span></div>
<div class="col-md-4"><input type="text" name="percentage" /></div>
</div><!--row ends-->
<div class="row" id="enquirynew">
<div class="col-md-2">Skills</div>
<div class="col-md-4"><input type="text" name="skills" /></div>
<div class="col-md-2">Certification</div>
<div class="col-md-4"><input type="text" name="certification"/></div>
</div><!--row ends-->
<div class="row" id="enquirynew">
<div class="col-md-2"> Country<span style="color:red;font-size:20px;">*</span></div>
<div class="col-md-4">
<select name="Country">
<option value="-1" selected>[choose yours]</option>
<option value="1">USA</option>
<option value="2">UK</option>
<option value="3">INDIA</option>
</select>
</div>
</div><!--row ends-->
<div class="row" id="enquirynew">
<div class="col-md-4 col-md-offset-4" >
<input type="submit" name="submit" value="Submit" style="background:orange;padding:10px 40px;text-align:center;"/><input type="button" value="Cancel" style="background:orange;padding:10px 40px;text-align:center;margin-left:5px;"/></div>
</div>
</form>
<?php
$wpdb;
if(isset($_POST['submit']))
{
$Name = $_POST["Name"];
$EMail = $_POST["EMail"];
$Phone = $_POST["Phone"];
$dateofbirth=$_POST["dateofbirth"];
$college = $_POST["college"];
$department = $_POST["department"];
$country = $_POST["country"];
$certification=$_POST["certification"];
$skills=$_POST["skills"];
$percentage=$_POST["percentage"];
$yearofpassedout=$_POST["yearofpassedout"];
if($wpdb->insert('student_profile',array("Name"=> $Name,"EMail"=>$EMail,"Phone"=>$Phone,"dateofbirth"=>$dateofbirth,"college"=>$college,"department"=>$department,"yearofpassedout"=>$yearofpassedout,"percentage"=>$percentage,"skills"=>$skills,"certification"=>$certification,"country"=>$country),array("%s","%s","%d","%s","%s","%s","%s","%s","%s","%s","%s")))
{
echo "insert succesfully";
}
else{
echo "there is error in insert query.";
}
}
?>
</div><!--TAB CONTENT ENDS HERE-->
<?php get_footer();?>
for this, you must use isset()
, The isset ()
function is used to check whether a variable is set or not. 为此,必须使用isset()
, isset ()
函数用于检查是否设置了变量。
<?php
if(isset($_POST['submit']))
{
$Name = $_POST["Name"];
$EMail = $_POST["EMail"];
$Phone = $_POST["Phone"];
$dateofbirth=$_POST["dateofbirth"];
$college = $_POST["college"];
$department = $_POST["department"];
$country = $_POST["country"];
$certification=$_POST["certification"];
$skills=$_POST["skills"];
$percentage=$_POST["percentage"];
$yearofpassedout=$_POST["yearofpassedout"];
$wpdb->insert('student_profile',array("Name"=> $Name,"EMail"=>$EMail,"Phone"=>$Phone,"dateofbirth"=>$dateofbirth,"college"=>$college,"depar tment"=>$department,"yearofpassedout"=>$yearofpassedout,"percentage"=>$percentage,"skills"=>$skills,"certification"=>$certification,"country"=>$country),array("%s","%s","%d","%s","%s","%s","%s","%s","%s","%s","%s"));
}
?>
now your code only execute when the submit button will hit.. 现在,您的代码仅在点击提交按钮时执行。
Thanks... 谢谢...
<?php get_header();?>
<script type="text/javascript">
<!--
// Form validation code will come here.
function validate()
{
if( document.myForm.Name.value == "" )
{
alert( "Please provide your name!" );
document.myForm.Name.focus() ;
return false;
}
if(document.myForm.percentage.value =="")
{
alert("please fill the percentage value");
document.myForm.percentage.focus();
return false;
}
if( document.myForm.EMail.value == "" )
{
alert( "Please provide your Email!" );
document.myForm.EMail.focus() ;
return false;
}
if( document.myForm.Phone.value == "" ||
isNaN( document.myForm.Phone.value ) ||
document.myForm.Phone.value.length != 10 )
{
alert( "Please provide a valid phone number" );
document.myForm.Phone.focus() ;
return false;
}
if( document.myForm.Country.value == "-1" )
{
alert( "Please provide your country!" );
return false;
}
return true;
}
//-->
</script>
<?php
if(isset($_POST['submit']))
{
global $wpdb;
$Name = $_POST["Name"];
$EMail = $_POST["EMail"];
$Phone = $_POST["Phone"];
$dateofbirth=$_POST["dateofbirth"];
$college = $_POST["college"];
$department = $_POST["department"];
$country = $_POST["country"];
$certification=$_POST["certification"];
$skills=$_POST["skills"];
$percentage=$_POST["percentage"];
$yearofpassedout=$_POST["yearofpassedout"];
if($wpdb->insert('student_profile',array("Name"=> $Name,"EMail"=>$EMail,"Phone"=>$Phone,"dateofbirth"=>$dateofbirth,"college"=>$college,"department"=>$department,"yearofpassedout"=>$yearofpassedout,"percentage"=>$percentage,"skills"=>$skills,"certification"=>$certification,"country"=>$country),array("%s","%s","%d","%s","%s","%s","%s","%s","%s","%s","%s")))
{
echo "insert succesfully";
}
else{
echo "there is error in insert query.";
}
}
?>
<form name="myForm" method="post" onsubmit="return(validate());">
<div class="row" id="enquirynew">
<div class="col-md-2" >name<span style="color:red;font-size:20px;">*</span></div>
<div class="col-md-4"><input type="text" name="Name" /></div>
<div class="col-md-2" >email<span style="color:red;font-size:20px;">*</span></div>
<div class="col-md-4"><input type="text" name="EMail" /></div>
</div>
<div class="row" id="enquirynew">
<div class="col-md-2" >Phone<span style="color:red;font-size:20px;">*</span></div>
<div class="col-md-4"><input type="text" name="Phone" /></div>
<div class="col-md-2" >Date of birth</div>
<div class="col-md-4">
<input type="text" name="dateofbirth" />
</div>
</div>
<div class="row" id="enquirynew">
<div class="col-md-2" >College</div>
<div class="col-md-4">
</div>
</div>
<div class="row" id="enquirynew">
<div class="col-md-2">Year of Passed out</div>
<div class="col-md-4"><input type="text" name="yearofpassedout" /></div>
<div class="col-md-2">Percentage<span style="color:red;font-size:20px;">*</span></div>
<div class="col-md-4"><input type="text" name="percentage" /></div>
</div><!--row ends-->
<div class="row" id="enquirynew">
<div class="col-md-2">Skills</div>
<div class="col-md-4"><input type="text" name="skills" /></div>
<div class="col-md-2">Certification</div>
<div class="col-md-4"><input type="text" name="certification"/></div>
</div><!--row ends-->
<div class="row" id="enquirynew">
<div class="col-md-2"> Country<span style="color:red;font-size:20px;">*</span></div>
<div class="col-md-4">
<select name="Country">
<option value="-1" selected>[choose yours]</option>
<option value="1">USA</option>
<option value="2">UK</option>
<option value="3">INDIA</option>
</select>
</div>
</div><!--row ends-->
<div class="row" id="enquirynew">
<div class="col-md-4 col-md-offset-4" >
<input type="submit" name="submit" value="Submit" style="background:orange;padding:10px 40px;text-align:center;"/><input type="button" value="Cancel" style="background:orange;padding:10px 40px;text-align:center;margin-left:5px;"/></div>
</div>
</form>
</div><!--TAB CONTENT ENDS HERE-->
<?php get_footer();?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.