This question already has an answer here:
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>
Return-path: $email";
$subject="Message sent using your contact form";
mail("myemail@email.com", $subject, $message, $from);
echo "Email sent!";
}
}
?>
</div>
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>
Return-path: $email";
$subject="Message sent using your contact form";
mail("aridjemana@email.com", $subject, $message, $from);
echo "Email sent!";
}
endif;
?>
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
Check to see if the index action
is set in $_REQUEST. And use empty()
to check if its set and empty.
<?
if(isset($_REQUEST['action']) && $_REQUEST['action']!="")
{
//do stuff
}
else
{
?>
<!-- show form --->
<?php
}
?>
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
}