How are scripts run?

Let’s say I call one script from another. And the called script uses a POST button. Both of these scripts read/write data to a MySQL database.

What I’m wondering is when the POST button is clicked in the browser, is the script executed from the very beginning? I ask because a $_GET variable is read correctly when the script is first called from the other script but that same variable is empty after the POST click

Depends on how you have it coded. If your using a button, the php needs to be inside an if statement, something like [php]if(isset($_POST[‘submit’])) {
// code
}[/php]If its not wrapped like that, it’ll execute when the page is loaded or refreshed. And that’s fine for db connection scripts, setting up defaults, doing global definitions (define()), etc.

Ok, what about something like this:

[php]<php?

//$id is set when the file is first called and is the result of a MySQL fetch later in the file
$id = $_GET[‘id’];

print '$id ’ . “$id\n”

if(isset($_POST[‘submitted’])){
print '$id ’ . “$id”;
exit();
}

/*
Block that reads in database which includes $id field
*/

print "<input type=“submit” value=“Save” name=“submitted”> ;

?>
[/php]

$id contains the proper data when file is first called. When I click on the Save button, $id is empty. Hence my question

Yea, I know I had the <?php header wrong before in my previous post but it is correct in the actual file on the server

The first instance of $id is being executed when the page comes up, if you want to save that id for future use in a form, then you need to create a hidden input. take out exit() too, all it does is stop the script from running and the page from loading properly.

Well… this form is a popup that is supposed to close after the Save button is clicked.

Thanks for the hidden input suggestion, I will try that

Well, that didn’t seem to make any difference - $id is still empty…

Here’s the line I added:

[php]<input type=“hidden” value=“ID” name=“id”>

[/php]

The value is wrong. You need to echo $id.

value or name???

Before you hit submit, view the source code for the page and make sure that hidden input has a value (right click, view source)

This is really strange. I have other pop up forms that all this $id magic isn’t needed but this one form is the only problem one

Source shows $id is correct (776 in this case). But it still doesn’t pass it…

As a test, I wrote the following standalone code (I figured it would be a good way to get a handle on this problem:

[php]<?php

$id = 776;

print '$id ’ . “$id\n”;

if(isset($POST[‘cancel’])){
print “Exiting”;
}

if(isset($_POST[‘submitted’])){
print '$id ’ . “$id”;
}
print "
<input type=“submit” value=“Save” name=“submitted”>
<input type=“submit” value=“Cancel” name=“cancel” >
";
?>
[/php]

The page displays as I’d expect (and shows $id 776) but clicking either button does nothing. I’m obviously doing something wrong

Is that your entire code? Where is your form tag?

I figured he just didn’t show it, but yeah, the form tag is needed.

Yea, I didn’t include the form tags and once I included them, the buttons (of course) started working. But back to my original question.

When the Save button is pressed, is the entire script run again from the beginning? That is to say, for example… is the line $id = 776; read again?

Yes, each time you press the submit button, anything contained in that if statement is going to execute.

Found the problem.

I adapted this code from someone else and they were assigning an action to the form that didn’t direct the submit to the proper form (this one). Basically it was a URL redirect error.

Thanks for the help and sorry to waste your time

not a problem, glad you found the problem.

Sponsor our Newsletter | Privacy Policy | Terms of Service