-3

I have variables $a and $location. However when I press the 'Submit' button the variable has gone out of scope. I can pass in in the variable from input box T2 but I can't manage to manage to do it to 'a' which is a regular variable. I added echo $b to debug it and I do get that output which verifies the conditional statement is true.

<?php
[...]

$a = '5';
$location = 'home';

if(isset($_POST['submit'])) {
    $location=$_POST['T2'];
    echo $location;
    echo $a;
    $b = '6';
    echo $b;
    [...]
}
?>

<input type="text" name="T2" value="<?php echo $location; ?>">
<button type="submit" name="submit" value="create">Submit</button>
11
  • 2
    Is $a even defined? Commented Oct 9, 2014 at 15:52
  • 1
    What do you expect this snippet to do, and what does it actually do? This is not entirely clear to me. Commented Oct 9, 2014 at 15:53
  • 1
    @stevo You're going to have to show us more code then. This isn't enough to go on. It's anybody's guess. Plus, make sure your submit button's named and all elements are too. Commented Oct 9, 2014 at 15:55
  • 1
    a condition (if/else) structure doesnt create a new scope) it's not possible that the first $a and the 2nd $a have different values with above code unless $a is callable and even then. Commented Oct 9, 2014 at 15:56
  • 1
    This is just a snippet of my code. I omitted additional code to simply the code excerpt. I bet that omitted code holds the key to this answer if, according to your update, the conditional statement is true :) Commented Oct 9, 2014 at 16:43

3 Answers 3

1
$a="Testing";
echo $a;

if(isset($_POST['submit'])) {         // you're not getting here
    $location=$_POST['T2'];     
    echo $a;
    echo "Testing one two three";    // even this won't show :)
}

There is only one plausible reason for that second echo not to work, i.e. Your if condition doesnt evaluate to true. There is no other reason that can cause that. Now check your field or button named submit on the form :)

A simple print_r($_POST); will tell you all that was posted, you can check there if submit was also posted.

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

6 Comments

Well not really, the only way possible if there is no post with submit
That's the same statement in different words.
@DarkMukke Yes, if isset($_POST['submit']) doesn't return TRUE, then $a will never try and be echo'd.
@DarkMukke You're just regurgitating the answer but in different words... And I don't think you even realise you are.
@DarkMukke That's not the question, Question is about OP's confusion about scope and mistakenly thinking the variable is being eaten alive by the scope. Im not going to teach then which variable to check the form for.
|
1

Far as I can tell, you're missing the form tags along with the method which should be post, least from what you posted for code.

Sidenote: If what you posted isn't full code, do. The following works which printed home56 on submit.

I do believe that's what the expected result should be.

<?php

$a = '5';
$location = 'home';

if(isset($_POST['submit'])) {
    $location=$_POST['T2'];
    echo $location;
    echo $a;
    $b = '6';
    echo $b;

}
?>

<form action="" method = "post">
<input type="text" name="T2" value="<?php echo $location; ?>">
<input type="submit" name="submit" value="create">

</form>

Sidenote: You can keep your present button if you wish instead of the input I tested with:

<button type="submit" name="submit" value="create">Submit</button>

Footnotes:

Both [...] - any relevance? I'll bet there is.

6 Comments

I already had the following: <form id="form" name="form" method="post" action="index.php">
@stevo My answer works. If it's still not working for you, then you have to show us full code. There's nothing more that Hanky or I can do without knowing what's in [...] or elsewhere.
@stevo Add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); see if it yields anything, and use var_dump(); on your variables. If you have any JS happening, let us know. You really need to post full code right up front, because it's guess work right off the bat and I don't deal with guesses, I deal with actuals. Had I known beforehand, I would not have submitted my answer.
@stevo From what I gather in your other question stackoverflow.com/q/26278179 - you most likely do have JS happening <body onload="document.FormName.reset();"> am I right on this one? Relevant?
@stevo Thanks, but there's no way I can test this. See if someone else picks up on the question.
|
0

Got it to work by echoing it in a hidden input

<input type="hidden" name="myvar" value="<?php echo $a; ?>">

and then using $_POST to extract it

$a=$_POST['myvar'];

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.