PHP格式中未定义$ error

时间:2017-05-16 07:32:07

标签: php html ajax forms

正如标题所暗示的,我在php中有一个简单的表单,它不断给出一个对象$ error的未定义错误。我已经尝试全局声明它并将其设置为null但所有这一切都是为了防止任何错误日志。代码如下,谢谢!

validation_size = 0.20
seed = 10
X_train, X_validation, Y_train, Y_validation =   model_selection.train_test_split(X, Y, test_size=validation_size, random_state=seed)
seed = 10
scoring = 'accuracy'

kfold = model_selection.KFold(n_splits=10, random_state=seed)

cv_results = model_selection.cross_val_score(model, X_train, Y_train,cv=kfold, scoring=scoring)  #error occurs here

2 个答案:

答案 0 :(得分:0)

将错误变量声明为数组。将这些新行添加到您的代码中以修复

if($_POST) {
$error = array();#declare error variable   

 if (!isset($error)) {#check if we have an error message of some kind
     #proceed

答案 1 :(得分:0)

将$ error创建为空白数组。使用下面的代码。

    <?php


$siteOwnersEmail = 'myemail@yahoo.com';
$error=array();

if(!empty($_POST)) {

   $name = trim(stripslashes($_POST['contactName']));
   $email = trim(stripslashes($_POST['contactEmail']));
   $subject = trim(stripslashes($_POST['contactSubject']));
   $contact_message = trim(stripslashes($_POST['contactMessage']));

   // Check Name
    if (strlen($name) < 2) {
    $error['name'] = "Please enter your name.";
}
    // Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
    $error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
    $error['message'] = "Please enter your message. It should have at least 15 characters.";
}
  // Subject
  if ($subject == '') { $subject = "Contact Form Submission"; }


 // Set Message
  $message .= "Email from: " . $name . "<br />";
  $message .= "Email address: " . $email . "<br />";
  $message .= "Message: <br />";
  $message .= $contact_message;
  $message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";

  // Set From: header
  $from =  $name . " <" . $email . ">";

  // Email Headers
    $headers = "From: " . $from . "\r\n";
    $headers .= "Reply-To: ". $email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


   if (!$error) {

  ini_set("sendmail_from", $siteOwnersEmail); // for windows server
  $mail = mail($siteOwnersEmail, $subject, $message, $headers);

    if ($mail) { echo "OK"; }
  else { echo "Something went wrong. Please try again."; }

} # end if - no validation error

else {

    $response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
    $response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
    $response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;

    echo $response;

} # end if - there was a validation error

}

?>