Reputation: 11
Error in PHP code. Still showing me this error "Undefined index: action" in php function $action = $_REQUEST['action']; Could you please help me to find out the solution.
thank you
full code here :
<?php
$action = $_REQUEST['action'];
if ($action== "") {
?>
<form action="" method="POST" >
Meno*:<br>
<input name="name" type="text" value="" size="30"/><br>
Email*:<br>
<input name="email" type="text" value="" size="30"/><br>
Vaša otázka*:<br>
<textarea name="message" rows="11" cols="80"></textarea><br>
<input type="submit" value="Poslať email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
if ( ($name=="") || ($email=="") || ($message=="") ) {
echo "Všetky políčka je potrebné vyplniť. Napíšte <a href=\"\"> otázku </a> ešte raz.";
} else {
$from = "From: $name<$email>\r\nReturn-path: $email";
$subject = "Message sent using your contact form";
mail("[email protected]", $subject, $message, $from);
echo "Mail poslaný!";
}
}
?>
Upvotes: 1
Views: 10866
Reputation: 672
The action inside will not be value in $_REQUEST or any other variable. The proper way to check for if a form has been submitted is by checking the $_SERVER["REQUEST_METHOD"] variable. Look for 'GET' or 'POST'. In your example, you'll be looking for the variable 'POST'
<?php
if($_SERVER['REQUEST_METHOD']=="POST"){
//yourhtml
}
else /* send the submitted data */
{
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
if ( ($name=="") || ($email=="") || ($message=="") ) {
echo "Všetky políčka je potrebné vyplniť. Napíšte <a href=\"\"> otázku </a> ešte raz.";
} else {
$from = "From: $name<$email>\r\nReturn-path: $email";
$subject = "Message sent using your contact form";
mail("[email protected]", $subject, $message, $from);
echo "Mail poslaný!";
}
}
?>
Upvotes: 0
Reputation: 44823
Replace this line:
$action = $_REQUEST['action'];
with this one:
$action = ( array_key_exists( 'action', $_REQUEST) ? $_REQUEST['action'] : "" );
Also, as others have pointed out, there is no action variable defined in your form. I think you are trying to catch the action parameter from your <form>
tag, but that is not actually submitted to the server; that just tells the browser where to send the POST
data. So, instead of looking for $_REQUEST['action']
, try looking for $_REQUEST['name']
or one of the other fields actually defined in your form.
Upvotes: 4
Reputation: 9713
This error occurs because there's no form element named 'action
'.
Try test if form is submit. Simply add a name to submit button.
<input type="submit" value="Poslať email" name="send" />
Then test if it is clicked to send the form:
<?php
if ( !isset( $_POST['send'] ) ) {
// HTML code to display form
} else {
// send the submitted data
}
?>
Upvotes: 1
Reputation: 2115
You are trying to access a form element named action, but there is no such element. If you are trying to see if the script is being called by GET (to present the form) or POST (to process the form) try using something like:
if( $_SERVER['REQUEST_METHOD'] == 'GET' ) )
Instead of your current two lines:
$action = $_REQUEST['action'];
if ($action== "")
Upvotes: 0