php - Why would a variable change before its supposed to when a user clicks on post?

573

I have a variable that seems to change when the user clicks on post. I have read that post and gets are supposed to be at the top of a php page but i cant do that because i need variables from a direcortytreeiterator to work with the post. So the directorytreeiterator is before posts and so are the buttons but thats it.

What happens is, when the user clicks post, its supposed to use the variable to do a mysql statement. But through checking, when the user hits the button and post is initiated, the variable is set to the following pages's value before the statement is initiated. I had thought that the script inside post would execute before the new page loads but thats not what im seeing. Here's the code in order from top to bottom:

<button type="submit" form="form1" name="atoz" value="atoz">A to Z</button>

echo $var; //at this stage $var = images
if(isset($_POST['atoz'])) 
{
$sort_by = "atoz" ; 
echo $var; //at this stage $var seems to be a value of another directory that is expected only after page reloads
$sort = 1;
$stmt = $mysqli->prepare("UPDATE ".$tablename." SET sort_by=? WHERE sort=? AND sort_folder=? ") ;
$stmt->bind_param(" sis", $sort_by, $sort, $var) ;
$stmt->execute() ;
$stmt->close() ;
echo "<meta http-equiv='refresh' content='0'>" ; //loads new page where the $var should change to another directory
    } 

So $var is supposed have a value of "images" but when the user hits post the value through some testing seems to be the values it would have if it were on the next page, which is another directory (the page reloads the default page when http equiv is loaded). But this doesnt seem to be in order.

Also, when i set the $var to "images" inside the post brackets, it works. So that means my sql statement is working just fine. Why would a variable change like that before the code to change it is executed? Cheers for your help.

People are also looking for solutions to the problem: file - PHP Post Request with CURL - image + array

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.