php/javascript邮件表单索引错误

Undefined Index error for a php/javascript mail form

本文关键字:表单 索引 错误 javascript php      更新时间:2023-09-26

不知道我做错了什么。我设置了一个简单的php邮件表单,使用javascript验证。我干这行没多久了,希望你能帮帮我。我得到这样的错误,如"通知:未定义的索引:cr[first_name] in/home/content/17/8543117/"

This is the form processing script: 
$to = "alexmoverguz@gmail.com";
$subject = "You have a new lead!";
$first_name = $_POST['cr[first_name]'];
$last_name = $_POST['cr[last_name]'];
$phone = $_POST['cr[phonecr]'];
$phoneprefix = $_POST['cr[phonecr2]'];
$phonesuffix = $_POST['cr[phonecr3]'];
$email = $_POST['cr[email]'];
$zip = $_POST['cr[zip]'];
$message = '';
$headers = "From: $first_name 'n'n $last_name 'n'n $phone 'n'n $phoneprefix 'n'n      
$phonesuffix 'n'n $email 'n'n $zip";
$sent = mail($to,$subject,$headers);
if($sent)
{
$_SESSION['success'] = '<p class="error">Your inquiry has been sent!</p>';
header('Location: index.php?e=inquiry_sent'); 
exit();
}
?>

格式是:

<?php
if (isset($_REQUEST['e']))
if ( $_REQUEST['e']=="inquiry_sent"){
echo "<p class='"error'">Thanks! We will contact you shortly!</p>";
}else{
echo "<p class='"error2'">Your inquiry failed, please try again</p>";
}
?>
<form name="cr" action="thankyou.php" method="post" onSubmit="return form_check();">
    <input type="hidden" name="creditoptin" value="yes" />
    <div id="cform">
        <div class="crp">
            <label for="first_name">First Name</label>
            <input id="first_name" type="text" name="cr[first_name]" 
value="" style="width:160px;" />   
        </div>
        <div class="crp">
            <label for="last_name">Last Name</label>
            <input id="last_name" type="text" name="cr[last_name]"   
value="" style="width:160px;" />
        </div>
        <div class="crp">
            <label for="phonecr">Phone</label>
            <input name="cr[phonecr]" style="width:34px;" id="phonecr"       
  maxlength="3" onkeyup="force_numeric(this);autotab(this,'phonecr2');" value="" /> -
            <input name="cr[phonecr2]" style="width:34px;"  
id="phonecr2" maxlength="3" onkeyup="force_numeric(this);autotab(this,'phonecr3');"   
value="" /> -
            <input name="cr[phonecr3]" style="width:44px;"   
id="phonecr3" maxlength="4" onkeyup="force_numeric(this);" value="" />
        </div>
        <div class="crp">
            <label for="email">Email Address</label>
            <input id="email" type="text" name="cr[email]" maxlength="60" value="" style="width:160px;" />
        </div>
        <div class="crp">
            <label for="zip">Zip Code</label>
            <input id="zip" name="cr[zip]" maxlength="5"  
onkeyup="force_numeric(this)" value="" />
        </div>
        <div id="submt">
            <input type="image" src="images/submit.png" value="Submit"  
onclick="if(typeof sub_pop == 'function')sub_pop();" />
</form>

谢谢你们了!

您应该使用$first_name = $_POST['first_name'];而不是$first_name = $_POST['cr[first_name]'];。只有输入元素的名称被翻译成$_POST变量,没有表单的名称。

同样,你没有在你的代码中做任何输入消毒,而只是对用户在你的表单中输入的任何内容进行邮寄;这可能存在安全风险。您应该查看有关XSS和SQL注入等主题的Web,并在进一步处理用户输入之前对其进行消毒。

我不明白,错误告诉你到底是什么错了。将您的输入名称更改为first_name并检索POST,它显然不像cr[first_name]