0

I am passing a hidden variable from HTML to a PHP page. In the PHP page I want to use that variable in my form.

<?php               
   $newVar=trim($_POST['newVar']);
   $subject = "new var is ---> $newVar";
?>

I am not able to use $newVar in any if statement - it shows blank. If I try to echo $subject, it shows the value of $newVar but when I try to echo the value of $subject in any if statement it does not show me the value of $newVar.

My Code is: html markup is:

<a href="#" class="applyNow"  onclick="document.sendVar.newVar.value='myVar'; document.sendVar.submit(); return false">New Variable</a>

<form method="post" name="sendVar" action="test.php" style="display:none;">
    <input type="hidden" name="newVar" value="">
    <input type="submit" value="Send form!">
</form>

php:

<?php               
   $newVar=trim($_POST['newVar']);
   $subject = "new var is ---> $newVar";
?>


if(isset($_POST['submit'])) {
    if(!isset($hasError)) {
       $from_add = "[email protected]";         
       $emailTo = '[email protected]';                 
       $headers = "From: $from_add \r\n";
       $headers .= "Reply-To: $from_add \r\n";
       $headers .= "Return-Path: $from_add\r\n";
       $headers .= "X-Mailer: PHP \r\n";

       mail($emailTo, $subject, $headers);
 // HERE in $subject.. value of newVar is not displaying


       $emailSent = true;
     }

    $from_add = "[email protected]";     
    $headers = "From: $from_add \r\n";
    $headers .= "Reply-To: $from_add \r\n";
    $headers .= "Return-Path: $from_add\r\n";
    $headers .= "X-Mailer: PHP \r\n";
}
?>
2
  • 6
    Give us an example of your if statement Commented Apr 18, 2012 at 12:30
  • 3
    what if statements are you using? it would be easier to debug if you could post the actual code you are using and then we can see what you are trying to do. Also, post the html markup for the hidden input aswell from your form page. Commented Apr 18, 2012 at 12:30

1 Answer 1

3

You seem to have ended a PHP code section with ?> and not started a new one with <?php - although I shall assume that this is simply a copy/paste error since you are not complaining that you can see your PHP source code in the browser.

The actual problem you have here is that you are checking if $_POST['submit'] is set, but it never will be because you didn't name any of your form controls submit, so none of the code in the if block will ever be executed.

In your HTML, change:

<input type="submit" value="Send form!">

...to:

<input type="submit" name="submit" value="Send form!">

...and it should work.

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

3 Comments

thanks for your reply.. I have a form in php file, code is huge so could not paste that code here.. so it is checking $_POST['submit'] for that form. If I try to echo $subject in beginning i.e. immediately after declaring $subject, it does show me the value of my post variable. But the same disappears when used in following if statements.. hope I am clear..
Either you are not entering the if block because the condition has not been met, or you have reassigned $newVar somewhere further up the code. Check for comparisons that are missing an equals sign, e.g. check you don't have if ($newVar = 1) where it should be if ($newVar == 1)
I am entering in the if block because I can see other text apart from $newVar value.. Also, Instead of $_POST variable, I tried to give explicitly different value.. meaning instead of $newVar=trim($_POST['newVar']); i tried to use $newVar="test"; . In this scenario.. it shows me $newVar value in $subject. So I assumed the issue is with post variable only..

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.