0

Hi guys having little problem with a form....when publish to site I get this error (Notice: Undefined index:), it works but just error above actual form, singled out the top part of the form below where its saying where error is......

<?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("myemail@email.com", $subject, $message, $from);
    echo "Email sent!";
    }
}  
?>
Guns
  • 2,678
  • 2
  • 23
  • 51
1966
  • 27
  • 1
  • 7

5 Answers5

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

   <?php
}
?>
Rubensito
  • 102
  • 6
0

the notices comes cause values not found or variables not initialized. so try to initialize vars or use check for values

use empty() or isset() for check values like :-

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

if(!empty($action))
 // do your stuff
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
0

Check to see if the index action is set in $_REQUEST. And use empty() to check if its set and empty.

Teena Thomas
  • 5,139
  • 1
  • 13
  • 17
0

You are checking form submission in wrong manner

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

Asssuming, the button name, from which the control is coming is name="submit_form" and method is POST

use :

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
}
NoobEditor
  • 15,563
  • 19
  • 81
  • 112
0

Change your code with this... you should use empty function in php to check variabel is empty or not. because index action in $_REQUEST['action'] is never exist when the first code is running.

<?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("aridjemana@email.com", $subject, $message, $from);
    echo "Email sent!";
    }
endif;
?> 
Ari Djemana
  • 1,229
  • 12
  • 12
  • This has worked, couldn't get my head round how to write it properly, thanks everyone! html, yes...PHP is another animal! – 1966 Apr 24 '14 at 13:51