2014-04-24 58 views
0

嗨,大家有一個問題的形式....當發佈到網站我得到這個錯誤(注意:未定義的索引:),它的工作原理,但只是錯誤上面的實際形式,挑出頂部下面的地方說那裏是錯誤的形式......PHP表格錯誤,新手

<?php 
$action=$_REQUEST['action']; 
if ($action=="") /* display the contact form */ 
{ 
?> 



<?php 
$action=$_REQUEST['action']; 
if ($action=="") /* display the contact form */ 
{ 
?> 
<form action="" method="POST" enctype="multipart/form-data"> 
<input type="hidden" name="action" value="submit"> 
Your name:<br> 
<input name="name" type="text" value="" size="30"/><br> 
Your email:<br> 
<input name="email" type="text" value="" size="30"/><br> 
Your message:<br> 
<textarea name="message" rows="7" cols="30"></textarea><br> 
<input type="submit" value="Send email"/> 
</form> 
<?php 
} 
else    /* send the submitted data */ 
{ 
$name=$_REQUEST['name']; 
$email=$_REQUEST['email']; 
$message=$_REQUEST['message']; 
if (($name=="")||($email=="")||($message=="")) 
    { 
    echo "All fields are required, please fill <a href=\"\">the form</a> again."; 
    } 
else{   
    $from="From: $name<$email>\r\nReturn-path: $email"; 
    $subject="Message sent using your contact form"; 
    mail("[email protected]", $subject, $message, $from); 
    echo "Email sent!"; 
    } 
} 
?> 

回答

0

更改您的代碼與此... 你應該在php中使用空函數來檢查變量是否爲空。因爲當第一個代碼運行時,$ _REQUEST ['action']中的索引操作永遠不會存在。

<?php 
$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : ""; 
if ($action==""): /* display the contact form */ 
?> 



<?php 
$action= (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : ""; 
if ($action==""): /* display the contact form */ 
?> 
<form action="" method="POST" enctype="multipart/form-data"> 
<input type="hidden" name="action" value="submit"> 
Your name:<br> 
<input name="name" type="text" value="" size="30"/><br> 
Your email:<br> 
<input name="email" type="text" value="" size="30"/><br> 
Your message:<br> 
<textarea name="message" rows="7" cols="30"></textarea><br> 
<input type="submit" value="Send email"/> 
</form> 
<?php 
endif; 
else:    /* send the submitted data */ 
$name=$_REQUEST['name']; 
$email=$_REQUEST['email']; 
$message=$_REQUEST['message']; 

if (($name=="")||($email=="")||($message=="")) 
    { 
    echo "All fields are required, please fill <a href=\"\">the form</a> again."; 
    } 
else{   
    $from="From: $name<$email>\r\nReturn-path: $email"; 
    $subject="Message sent using your contact form"; 
    mail("[email protected]", $subject, $message, $from); 
    echo "Email sent!"; 
    } 
endif; 
?> 
+0

任何答案是有用的這工作,couldn不要讓我的頭腦如何正確書寫,謝謝大家! HTML,是的... PHP是另一種動物! – 1966

+0

我的榮幸.. :) –

0

的告示來未找到原因值或變量未初始化。所以儘量初始化瓦爾或使用檢查值

使用empty()isset()的校驗值 ,如: -

$action=(isset($_REQUEST['action']) ? $_REQUEST['action'] :''); 

if(!empty($action)) 
// do your stuff 
+0

謝謝你......好吧,我不知道如何代碼到這個... – 1966

+0

的歡迎:)如果你請接受或給予好評他們 –

0

檢查,看看是否索引action設置在$ _REQUEST。並使用empty()來檢查它的設置和空。

1
<? 
    if(isset($_REQUEST['action']) && $_REQUEST['action']!="") 
{ 
    //do stuff 
} 
else 
{ 
    ?> 
    <!-- show form ---> 

    <?php 
} 
?> 
0

要檢查表單提交以錯誤的方式

$action=$_REQUEST['action']; 
if ($action=="") /* display the contact form */ 

Asssuming,按鈕名稱,從控制來臨是name="submit_form"和方法POST

使用:

if(isset($_POST["submit_form"])) //this will check if "submit_form" was pressed or not 
{ 
//show form submission action 
} 
else 
{ 
//don't show show form submission action 
}