【问题标题】:PHP error notice on radio button单选按钮上的 PHP 错误通知
【发布时间】:2016-04-26 15:35:23
【问题描述】:

我正在使用 PHP 验证表单(非常非常基本的表单),我想知道是否有解决方案可以不出现此错误 注意:未定义索引:第 26 行 /directory/... 中的 sendMethod 这是我的表单代码:

<form name="myForm" id="userDetails" action="formProcess.php" onSubmit="return validateForm()" method="post">
  <fieldset class="custInf">
    <h3> User Details </h3>
    <label class="inputArea" for="fName">Forename :</label>
    <input type="text" name="forename" id="fname" placeholder="Enter First Name" maxlength="20" size="15">
    </input>
    <label class="inputArea" for="sName">Surname :</label>
    <input type="text" name="surname" id="surname" placeholder="Enter Last Name" maxlength="20" size="15">
    </input>
    <label class="inputArea" for="email">Email :</label>
    <input type="email" name="email" id="email" placeholder="Enter Email Address" maxlength="40" size="15" />
    </input>
    <label class="inputArea" for="hmph">Landline number :</label>
    <input type="tel" name="landLineTelNo" id="hmphone" placeholder="Enter Landline no." maxlength="11" size="15">
    </input>
    <label class="inputArea" for="mobileTelNo">Mobile number :</label>
    <input type="tel" name="mobileTelNo" id="mobile" placeholder="Enter Mobile no." maxlength="11" size="15">
    </input>
    <label class="inputArea" for="address">Postal Address :</label>
    <input type="text" name="postalAddress" id="address" placeholder="Enter House no." maxlength="25" size="15">
    </input>
  </fieldset>
  <fieldset class="contactType">
    <h3> How would you like to be contacted? </h3>
    <label class="radioBt" for="sms">SMS</label>
    <input id="smsBut" type="radio" name="sendMethod" value="SMS">
    </input>
    <label class="radioBt" for="email">Email</label>
    <input type="radio" name="sendMethod" value="Email">
    </input>
    <label class="radioBt" for="post">Post</label>
    <input type="radio" name="sendMethod" value="Post">
    </input>
  </fieldset>
  <fieldset class="termsCon">
    <input type="checkbox" name="check" value="check" id="check" />I have read and agree to the Terms and Conditions and Privacy Policy
    </input>
  </fieldset>
  <input class="submitBut" type="submit" name="submitBut" value="Submit" </input>
</form>
$fname = $_REQUEST['forename'];
if (empty($fname)) {
die("<p>Enter a first name</p>\n");
}

$surname = $_REQUEST['surname'];
if (empty($surname)) {
die("<p>You must enter a surname</p>\n");
}

$email = $_REQUEST['email'];
if (empty($email)) {
die("<p>You need to enter an email</p>\n");
}
//Landline not required so wont give error 
$hmphone = isset($_REQUEST['hmph']) ? $_REQUEST['hmph'] : null ;

$mobile = $_REQUEST['mobileTelNo'];
if (empty($mobile)) {
die("<p>Enter a Mobile Number</p>\n");
}

$address = $_REQUEST['postalAddress'];
if (empty($address)) {
die("<p>Enter your postal address</p>\n");
}

$sendMethod = $_REQUEST['sendMethod'];
if (empty($sendMethod)) {
die("<p>Please choose a contact option</p>\n");
}

$check = $_REQUEST['check'];
if (empty($check)) {
die("<p>Please select the Terms and Conditions to continue</p>\n");
}   

它是一个非常基本的 PHP 验证,我只是希望它在检查所有字段之前不会给我一个错误通知,例如,一旦所有字段都填写了“请选择一个联系选项”,错误就会出现但是,如果我填写了手机以外的所有字段,我会收到错误通知

【问题讨论】:

    标签: php html web


    【解决方案1】:

    为避免通知,请先检查是否为empty

    if (empty($_REQUEST['sendMethod']))
      die("<p>Please choose a contact option</p>\n");
    else
      $sendMethod = $_REQUEST['sendMethod'];
    

    【讨论】:

    • 好的,我仍然收到错误,但这是这行代码的原因 $sendMethod = $_POST['sendMethod'];
    • 秒我不知道如何在 cmets 中执行代码复制粘贴一些东西
    • //为安全转义用户输入 $forename = mysqli_real_escape_string($link, $_POST['forename']); $surname = mysqli_real_escape_string($link, $_POST['surname']); $email = mysqli_real_escape_string($link, $_POST['email']); $landLineTelNo = mysqli_real_escape_string($link, $_POST['landLineTelNo']); $mobileTelNo = mysqli_real_escape_string($link, $_POST['mobileTelNo']); $postalAddress = mysqli_real_escape_string($link, $_POST['postalAddress']); $sendMethod = $_POST['sendMethod'];
    • 好吧对不起,我能用最好的方式来描述我的意思,对不起,如果它看起来不清楚。当我提交带有所有其他字段的表单时,单选按钮会收到我设置的标准错误消息,但是一旦我到达未选中的单选按钮,我的错误就会显示为“通知:未定义索引:sendMethod in /第 56 行的 home/directory/file.php 请选择联系方式'
    • 为什么不编辑您的问题以包含您在此处评论的全部代码。有什么东西不见了。 if (empty($_REQUEST['sendMethod'])) die( 意味着它不会再进一步​​了,所以当你尝试$sendMethod = $_POST['sendMethod']; 时不可能得到未定义的变量通知稍后
    猜你喜欢
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 2013-08-06
    • 2016-01-23
    • 2014-03-26
    • 2011-08-30
    • 2019-04-27
    • 2020-03-18
    相关资源
    最近更新 更多