使用javascript在php联系表单中的必填字段

时间:2014-02-11 13:06:26

标签: javascript php validation

我需要在以下字段中强制使用此表单。

名称: 移动电话 电子邮件地址: 地址第一行: 地址第2行: 城市/城镇: 县: 邮编: 服务请求的问题: 项目/ PC型号: 设备类型: 发票编号:

我尝试使用JavaScript将它放在标题和索引文件中,但是无法看到让它们中的任何一个工作。任何想法或建议将不胜感激。我不想使用PHP或必须重新编码当前的联系表格。

PHP:

<?php

if (array_key_exists('func',$_REQUEST)) {
$func = $_REQUEST['func'];
} else {
$func = "";
}




function submitsq() {
require_once("header.php");

require_once("deps.php");

$rs_connect = @mysql_connect($dbhost, $dbuname, $dbpass) or die("Couldn't connect the db");
$rs_select_db = @mysql_select_db($dbname, $rs_connect) or die("Couldn't select the db");
mysql_query("SET NAMES utf8");

require_once("common.php");
if (array_key_exists('sent',$_REQUEST)) {
$sent = pv($_REQUEST['sent']);
if($sent == 1) {
echo "<br><div class=noticebox><font class=text16b>".pcrtlang("Thank You for contacting us. We will contact you as soon as possible").".<br><br>";
echo pcrtlang("If you have any other Service Requests, you may submit them below").".";
echo "</font></div><br>";
} else {
echo "<br><div class=noticebox><font class=text16b>".pcrtlang("Service Request Not Sent").".<br><br>";
echo "</font></div><br>";
}
}

$sreq_ip = $_SERVER['REMOTE_ADDR'];

$rs_chkip = "SELECT * FROM servicerequests WHERE sreq_ip = '$sreq_ip' AND sreq_datetime > (DATE_SUB(NOW(),INTERVAL $withindays DAY))";
$rs_result = mysql_query($rs_chkip, $rs_connect);
$totalsubs = mysql_num_rows($rs_result);

if($totalsubs < $maxcount) {
echo "<br><div class=box><font class=text16b>".pcrtlang("Submit Service Requests")."</font><br><br>";

echo "<form action=index.php?func=submitsq2 method=post name=myform>";
echo "<table style=\"width:100%;\"><tr><td style=\"vertical-align:top;width:50%;\"\"><table>";
echo "<tr><td><font class=text14>".pcrtlang("Your Name").":</font></td><td><input name size=26 required type=text name=sreq_name  onFocus=\"this.form.submitbutton.disabled=false;this.form.submitbutton.value='".pcrtlang("Submit Service Request")."';\"></td></tr>";
echo "<tr><td><font class=text14>".pcrtlang("Company").":</font></td><td><input size=26 type=text name=sreq_company></td></tr>";
echo "<tr><td colspan=2><font class=text10i><br>".pcrtlang("Please enter at least one phone number").".</font></td></tr>";
echo "<tr><td><font class=text14>".pcrtlang("Home Phone Number").":</font></td><td><input size=18 type=text name=sreq_phone></td></tr>";
echo "<tr><td><font class=text14>".pcrtlang("Mobile Phone Number").":</font></td><td><input size=18 type=text name=sreq_cellphone></td></tr>";
echo "<tr><td><font class=text14>".pcrtlang("Work Phone Number").":</font></td><td><input size=18 type=text name=sreq_workphone></td></tr>";

echo "<tr><td><font class=text14>".pcrtlang("Email Address").":</font></td><td><input size=18 type=text name=sreq_email></td></tr>";

echo "<tr><td><font class=text14>$pcrt_address1:</font></td><td><input size=24 type=text name=sreq_addy1></td></tr>";
echo "<tr><td><font class=text14>$pcrt_address2:</font></td><td><input size=24 type=text name=sreq_addy2></td></tr>";
echo "<tr><td><font class=text14>$pcrt_city:</font></td><td><input size=18 type=text name=sreq_city></td></tr>";
echo "<tr><td><font class=text14>$pcrt_state:</font></td><td><input size=6 type=text name=sreq_state></td></tr>";
echo "<tr><td><font class=text14>$pcrt_zip:</font></td><td><input size=10 type=text name=sreq_zip></td></tr>";

echo "</table></td><td>&nbsp;&nbsp;&nbsp;</td><td style=\"vertical-align:top\">";


echo "<font class=text14>".pcrtlang("Service Requested/Problem").":</font><br><textarea name=sreq_problem requires cols=40 rows=10 onFocus=\"this.form.submitbutton.disabled=false;this.form.submitbutton.value='".pcrtlang("Submit Service Request")."';\"></textarea>";
echo "<br><br><font class=text14>".pcrtlang("Item / PC Model Number: ie. Dell, Apple, HP, etc")."</font><br><input size=36 type=text name=sreq_model>";
echo "<br><br><font class=text14>".pcrtlang("Device Type: ie. Laptop, PC, Tablet, etc")."</font><br><input size=36 type=text name=sreq_type>";

echo "<br><br><font class=text14>".pcrtlang("Invoice Number")."</font><br><input size=36 type=text name=sreq_pcid>";


if($allowusertochoosestore == "yes") {
echo "<br><br><font class=text14>".pcrtlang("Preferred Store/Location").":</font><br><select name=storeid>";
echo "<option selected value=\"0\">".pcrtlang("No Preference")."</option>";
$rs_ql = "SELECT * FROM stores WHERE storeenabled = '1'";
$rs_result1 = mysql_query($rs_ql, $rs_connect);
while($rs_result_q1 = mysql_fetch_object($rs_result1)) {
$storeid = "$rs_result_q1->storeid";
$storename = "$rs_result_q1->storename";
$storesname = "$rs_result_q1->storesname";
echo "<option value=\"$storeid\">$storename - &lt;$storesname&gt;</option>";
}
echo "</select>";
} else {
echo "<input type=hidden name=storeid value=0>";
}
echo "</select><br>";


if($recaptcha_enable == "yes") {
echo "<br><br><font class=text14>".pcrtlang("Please enter the words below").":</font><br>";
require_once('recaptchalib.php');
echo recaptcha_get_html($recaptcha_public_key);
echo "<font class=text10>".pcrtlang("This little test is just to make sure you are a real person and not a spammer so that our oncall technician only receives mobile phone notifications for genuine service requests").".</font>";

}


echo "<br><br><input class=button id=submitbutton type=submit value=\"".pcrtlang("Submit Service Request")."\" onclick=\"this.disabled=true;this.value='".pcrtlang("Sending Request")."...'; this.form.submit();\">";

echo "</td></tr></table></div></form>";
} else {
echo "<br><div class=noticebox><font class=text16b>".pcrtlang("Sorry, the maximum number of requests has been reached").".<br><br>";
echo "</font></div><br>";

}

require_once("footer.php");

}


function submitsq2() {
require("deps.php");

$rs_connect = @mysql_connect($dbhost, $dbuname, $dbpass) or die("Couldn't connect the db");
$rs_select_db = @mysql_select_db($dbname, $rs_connect) or die("Couldn't select the db");
mysql_query("SET NAMES utf8");

require("common.php");

if (function_exists('date_default_timezone_set')) {
date_default_timezone_set("$pcrt_timezone");
}
$currentdatetime = date('Y-m-d H:i:s');

$sreq_name = pv($_REQUEST['sreq_name']);
$sreq_company = pv($_REQUEST['sreq_company']);
$sreq_phone = pv($_REQUEST['sreq_phone']);
$sreq_cellphone = pv($_REQUEST['sreq_cellphone']);
$sreq_workphone = pv($_REQUEST['sreq_workphone']);
$sreq_model = pv($_REQUEST['sreq_model']);
$sreq_type = pv($_REQUEST['sreq_type']);
$sreq_email = pv($_REQUEST['sreq_email']);
$sreq_addy1 = pv($_REQUEST['sreq_addy1']);
$sreq_addy2 = pv($_REQUEST['sreq_addy2']);
$sreq_city = pv($_REQUEST['sreq_city']);
$sreq_state = pv($_REQUEST['sreq_state']);
$sreq_zip = pv($_REQUEST['sreq_zip']);
$sreq_problem = pv($_REQUEST['sreq_problem']);
$sreq_storeid = pv($_REQUEST['storeid']);
$sreq_custsourceid = pv($_REQUEST['custsourceid']);
$sreq_pcid = pv($_REQUEST['sreq_pcid']);

$sreq_ip = $_SERVER['REMOTE_ADDR'];
$sreq_agent = $_SERVER['HTTP_USER_AGENT'];


if($recaptcha_enable == "yes") {
require_once('recaptchalib.php');
  $resp = recaptcha_check_answer ($recaptcha_private_key,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die (pcrtlang("The words in the puzzle were not entered correctly. Please go back and try it again."));
  } else {
    // Your code here to handle a successful verification
  }
}




$rs_chkip = "SELECT * FROM servicerequests WHERE sreq_ip = '$sreq_ip' AND sreq_datetime > (DATE_SUB(NOW(),INTERVAL $withindays DAY))";
$rs_result = mysql_query($rs_chkip, $rs_connect);
$totalsubs = mysql_num_rows($rs_result);

if($totalsubs >= $maxcount) {
header("Location: index.php?sent=0");
} else {

$rs_insert_sq = "INSERT INTO servicerequests (sreq_ip,sreq_agent,sreq_name,sreq_company,sreq_homephone,sreq_cellphone,sreq_workphone,sreq_addy1,sreq_addy2,sreq_city,sreq_state,sreq_zip,sreq_email,sreq_problem,sreq_model,sreq_datetime,storeid,sreq_custsourceid,sreq_pcid) VALUES ('$sreq_ip','$sreq_agent','$sreq_name','$sreq_company','$sreq_phone','$sreq_cellphone','$sreq_workphone','$sreq_addy1','$sreq_addy2','$sreq_city','$sreq_state','$sreq_zip','$sreq_email','$sreq_problem','$sreq_model $sreq_type','$currentdatetime','$sreq_storeid','$sreq_custsourceid','$sreq_pcid')";
@mysql_query($rs_insert_sq, $rs_connect);


if ($sreq_storeid != "0") {
$rs_qstores = "SELECT * FROM stores WHERE storeid = '$sreq_storeid'";
$rs_result1 = mysql_query($rs_qstores, $rs_connect);
$rrow = mysql_fetch_array($rs_result1);
$oncalluser = $rrow['oncalluser'];
} else {
$rs_qstoresid = "SELECT * FROM stores WHERE storedefault = '1'";
$rs_result1id = mysql_query($rs_qstoresid, $rs_connect);
$rrow = mysql_fetch_array($rs_result1id);
$oncallstoreid = $rrow['storeid'];
$rs_qstores = "SELECT * FROM stores WHERE storeid = '$oncallstoreid'";
$rs_result1 = mysql_query($rs_qstores, $rs_connect);
$rrow = mysql_fetch_array($rs_result1);
$oncalluser = $rrow['oncalluser'];
}


$rs_quser = "SELECT * FROM users WHERE username = '$oncalluser'";
$rs_resultuser1 = mysql_query($rs_quser, $rs_connect);
$urow = mysql_fetch_array($rs_resultuser1);
$useremail = $urow['useremail'];
$usermobile = $urow['usermobile'];


if(($useremail != "") && ($oncallsendemail == "yes")) {
require_once("sendenotify.php");
$from = "$useremail";
$to = "$useremail";
$subject = pcrtlang("Service Request Submitted");
$plaintext ="$sreq_name\n$sreq_company\n$sreq_addy1\n$sreq_addy2\n$sreq_city, $sreq_state $sreq_zip\n\n".pcrtlang("Home Phone").":\t$sreq_phone\n".pcrtlang("Cell Phone").":\t$sreq_cellphone\n".pcrtlang("Work Phone").":\t$sreq_workphone";
$plaintext .= "\n\n".pcrtlang("Email").": $sreq_email\n\n".pcrtlang("Device").": $sreq_model $sreq_type\n\n".pcrtlang("Problem").":\n$sreq_problem\n";
$sreq_problem2 = nl2br($_REQUEST['sreq_problem']);
$htmltext ="<b>$sreq_name</b><br>$sreq_name<br>$sreq_addy1<br>$sreq_addy2<br>$sreq_city, $sreq_state $sreq_zip<br><br><b>".pcrtlang("Home Phone").":</b>$sreq_phone<br><b>".pcrtlang("Cell Phone").":</b>$sreq_cellphone<br><b>".pcrtlang("Work Phone").":</b>$sreq_workphone";
$htmltext .= "<br><br><b>".pcrtlang("Email")."</b>:$sreq_email<br><br><b>".pcrtlang("Device").":</b> $sreq_model $sreq_type<br><br><b>".pcrtlang("Problem").":</b><br>$sreq_problem2<br><br>";
sendenotify("$from","$to","$subject","$plaintext","$htmltext");
}


if(($usermobile != "") && ($oncallsendsms == "yes")) {
require_once("smsnotify.php");
$smsname  = urlencode("$sreq_name");
$sms_cellphone  = urlencode("$sreq_cellphone");
smssend("$usermobile",pcrtlang("A Service Request has been submitted by")." $smsname - $sms_cellphone.");
}

header("Location: index.php?sent=1");

}
}

switch($func) {

    default:
    submitsq();
    break;

    case "submitsq2":
    submitsq2();
    break;

}

?>

JavaScript的:

<script language="javascript" 
type="text/javascript">

<!-- hide script from older browsers
function validateForm(myform)
{

if(""==document.forms.myform.sreq_name.value)
{
alert("Please enter your full name.");
return false;
}

if(""==document.forms.myform.sreq_cellphone.value)
{
alert("Please enter your email address.");
return false;
}

if(""==document.forms.contact.sreq_phone.value)
{
alert("Please enter your phone number.");
return false;
}

}
stop hiding script -->
</script>

1 个答案:

答案 0 :(得分:0)

对脚本的更正

<script type="text/javascript">
function validateForm(myform) {
  var ele = myform.elements
  if("" == ele['sreq_name'].value) {
    alert("Please enter your full name.");
    return false;
  }

  if("" == ele['sreq_cellphone'].value ) {
    alert("Please enter your email address.");
    return false;
  }

  if("" == ele['contact.sreq_phone'].value) {
    alert("Please enter your phone number.");
    return false;
  }
}
</script>

如果您的功能有任何错误,则表单会自动提交而无需验证。

您还需要在服务器端进行验证。