2013-05-08 4 views
0

HTML, PHP 및 부트 스트랩 트위터를 사용하여 웹 사이트 (http://youngliferaffle.com/)에 양식을 만들려고합니다. 나는 그것을 만드는 방법에 대한 여러 가지 자습서를 조사했지만, 사용자가 오류를 만들거나 답변을 제출 한 후에 사용자를 이전 할 때처럼 위치에 문제가 있다고 생각합니다. 나는 또한 PHP에 매우 익숙하다. 나는 정말 도움을 주셔서 감사합니다! 고맙습니다!HTML 및 PHP를 사용하여 양식 만들기

주요 문제 :

  • "웹 페이지에 리디렉션이 너무 많습니다"올바른 페이지로 리디렉션되지
  • 사용자 제출 또는 인한 후 제출로부터 이메일을 수신하지 쿠키
  • 에/가능성 때문에

    : 나쁜 제출 여기에

내 HTML 양식의 일부입니다

여기서 다시 같은 위치로 향하고 유지, 내 PHP 양식 맨 처음 줄에

// check for form submission - if it doesn't exist then send back to contact form 
if (!isset($_POST['save']) || $_POST['save'] != 'contact') { 
    header('Location: contact-form-submission.php'); 
    exit; 
} 

// get the posted data 
$name = $_POST['contact_name']; 
$email_address = $_POST['contact_email']; 
$phone = $_POST['contact_phone']; 

// check that a name was entered 
if (empty($name)) 
    $error = 'You must enter your name.'; 
// check that an email address was entered 
elseif (empty($email_address)) 
    $error = 'You must enter your email address.'; 
// check for a valid email address 
elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_address)) 
    $error = 'You must enter a valid email address.'; 
// check that a phone number was entered 
elseif (empty($phone)) 
    $error = 'You must enter a phone number.'; 

// check if an error was found - if there was, send the user back to the form 
if (isset($error)) { 
    //Am I putting the wrong location? 
    header('Location: contact-form-submission.php?e='.urlencode($error)); exit; 
} 

// write the email content 
$email_content = "Name: $name\n"; 
$email_content .= "Email Address: $email_address\n"; 
$email_content .= "Phone:\n\n$phone"; 

// send the email 
mail ("myemail.com", "New Contact Message", $email_content); 

// send the user back to the form 
//And This is where I'm having trouble with! the Location part 
header('Location: contact-form-submission.phps='.urlencode('Thank you for your message.')); 
exit; 
+0

그래서 어떤 특정 문제가 있습니까? –

+0

이 스크립트는 언제 실행됩니까? contact-form-submission.php, POST contact-form-submission.php 또는 다른 URL을 찾았습니까? – Mateusz

+0

정확하게 입력하지 않아도 양식을 제출하려고 시도하면 양식이 리디렉션되고 웹 페이지 (http://youngliferaffle.com/contact-form-submission.php)에 너무 많은 리디렉션이 있다고합니다. 또한, 정확하게 입력 했더라도 똑같다고 말하면서 이메일을받지 못합니다. – user2363671

답변

0

입니다. 이렇게하면 루프가 만들어집니다. 다시 양식으로 보내야합니다. 아마도 index.php?

또한 마지막 행은 데이터가 게시 될 때만이 페이지가 작동하므로이 페이지에서 사용자를 리디렉션해야합니다. 어쩌면 새로운 고맙습니다 페이지를 만들 수 있습니다.

기억 하시겠습니까? .php 후에? 웹 서버에 더 이상 파일 이름이 없음을 알립니다. 그렇지 않으면 thankyou.phps라는 파일을 찾습니다. 당신이 이름의 권리를 가지고있는 것처럼

// check for form submission - if it doesn't exist then send back to contact form 
if (!isset($_POST['save']) || $_POST['save'] != 'contact') { 
header('Location: index.php'); exit; 
} 

// get the posted data 
$name = $_POST['contact_name']; 
$email_address = $_POST['contact_email']; 
$phone = $_POST['contact_phone']; 

// check that a name was entered 
if (empty($name)) 
$error = 'You must enter your name.'; 
// check that an email address was entered 
elseif (empty($email_address)) 
$error = 'You must enter your email address.'; 
// check for a valid email address 
elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_address)) 
    $error = 'You must enter a valid email address.'; 
// check that a phone number was entered 
elseif (empty($phone)) 
$error = 'You must enter a phone number.'; 

// check if an error was found - if there was, send the user back to the form 
if (isset($error)) { 
//Am I putting the wrong location? 
header('Location: contact-form-submission.php?e='.urlencode($error)); exit; 
} 

// write the email content 
$email_content = "Name: $name\n"; 
$email_content .= "Email Address: $email_address\n"; 
$email_content .= "Phone:\n\n$phone"; 

// send the email 
mail ("myemail.com", "New Contact Message", $email_content); 

// send the user back to the form 
// remember the ? after your file name! 
header('Location: thankyou.php?s='.urlencode('Thank you for your message.')); exit; 
1

마지막 줄

header('Location: contact-form-submission.phps='.urlencode('Thank you for your message.')); exit; 

보인다. PHP 파일 자체를 리디렉션하는 이유는 무엇입니까? 이름이 무엇이든 초기 폼의 URL을 사용해야합니다. 아마이 줄은 당신이 얻는 리다이렉트 루프 오류를 만든다.

1

그럼 PHP 코드의 첫 번째 일은 자체로 리디렉션된다는 것입니다. 'save'변수는 어쨌든 설정되지 않으므로 여러 번 반복해서 리디렉션됩니다. '저장'이 설정되어 있는지 확인하지만 처음 설정되지 않았으므로 다시 같은 페이지로 리디렉션됩니다. 그러나 '저장'변수는 양식 제출이 아닌 리디렉션이기 때문에 다시 설정되지 않습니다. 그래서 반복해서 발생하므로 너무 많은 리디렉션 오류가 발생합니다.

저는 보통 처리 논리와 양식을 동일한 PHP 파일에 보관합니다. 즉, 양식의 액션 속성은 값과 동일한 페이지 URL을 갖게됩니다. 예를 들면 다음과 같습니다.

simple_form.php

<?php 
    //Assume the form has one field called field1 and a 'save' variable to indicate form submission 
    $field1 = ""; 

    //Declare an array to store errors 
    $errors = array(); 

    if(isset($_POST['save'])) { 
     //Form has been submitted.. do validations etc. 
     $field1 = $_POST['field1']; 
     if(someValidationCheck($field1) == false) { 
      $errors[] = "Field1 is not valid"; 
     } 

     //After all field validations.. adding errors to $errors array.. 
     if(count($errors) == 0) { 
      //No errors so write database insert statments etc. here 
      //Also put a header("Location:...") redirect here if you want to redirect to a thank you page etc. 
     } 
    } 
?> 
<html> 
<body> 
<?php 
    //If there were errors, show them here 
    if(count($errors) > 0) { 
     //loop through $errors array .. print one by one. 
    } 
?> 
<form method="post" action="simple_form.php"> 
    <input type="text" name="field1" value="<?php echo($field1); ?>" /> 
    <input type="hidden" name="save" value="save" /> 
    <input type="submit" /> 
</form> 
</body> 
</html> 

오류가있을 경우, 사용자가 필드는 원래 값을 유지합니다, 동일한 페이지에 오류 메시지가 표시됩니다 그런 식으로. 그리고 오류없이 유효한 제출시에만 리디렉션됩니다. 그렇지 않으면 오류 메시지가 표시되어 동일한 페이지에 남아 있습니다.

관련 문제