我想使用javascript,我的表单不会在刷新时通过空白字段向表添加任何数据,我的代码在这里
<form id="form1" name="form1" method="post" onsubmit="return validateForm()" action="">
Name <input type="text" name="name" /> <br />
Website <input type="text" name="website" /> <br />
Description <input type="text" name="description" /> <br />
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
global $wpdb;
$wpdb->query("insert into ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}')");
?>
答案 0 :(得分:2)
尝试:
<form id="form1" name="form1" method="post" onsubmit="return validateForm();" action="aaa">
Name <input type="text" class="txt" name="name" /> <br />
Website <input type="text" class="txt" name="website" /> <br />
Description <input type="text" class="txt" name="description" /> <br />
<input type="submit" id="submit" value="Submit"/>
</form>
<script type="text/javascript">
function validateForm(){
var arr = document.getElementsByClassName('txt');
var errors = '';
for(var i=0;i<arr.length;i++){
if(!arr[i].value){
errors += arr[i].getAttribute('name') + ' cannot be blank\n';
}
}
if(errors.length){
alert(errors);
return false;
}
}
</script>