1

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&#357; 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ý!"; 
        } 
     }   
?>
1
  • i think it means that there's no $_REQUEST['action'] being made or to receive..what should be the value of your action, where it came from? Commented Oct 31, 2013 at 0:25

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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&#357; 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
        }

?>

Comments

0

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== "")

2 Comments

the form method is POST <form action="" method="POST" >
@Alsemany - That's correct, but the OP appears to be trying to use the same script to respond to a GET by returning the HTML for the form. The logic implied by the test in the original code is "if this is NOT a posted form", in which case the form is returned. The appropriate way to test for that is as I have described.
0

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ý!"; 
    } 
 }   

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.