Jump to content

Passing a PHP defined variable to another PHP URL


jxfish2

Recommended Posts

I have a working PHP created webpage, that I want to give a data sort option to.

 

Currently, I am calling one of 6 different scripts, to accomplish this data sort routine.

 

If I can just figure out how to pass the sort parameter back to the script itself, so that it will reload the page, using the new sort variable, I can combine all 6 scripts into a single script, making it much easier to maintain.

 

My current button selection code looks like this:

 

<td><form action="Get_Old_Assets_By_Alt.php" method="post"><input type="submit" value="Sort By Alt Code"></input></form></td><td><form action="Get_Old_Assets_By_Title.php" method="post"><input type="submit" value="Sort By Title"></input></form></td><td><form action="Get_Old_Assets_By_Prov.php" method="post"><input type="submit" value="Sort By Provider"></input></form>

</td>

 

I want to combine all of the code under a single script called "Get_Old_Assets.php", but I need to pass in one of the following variables:

 

alt

title

prov

 

I already tried the following:

 

<td><form action="Get_Old_Assets.php?alt" method="post"><input type="submit" value="Sort By Alt Code"></input></form>

</td>

But, I can't seem to get the script to read in the URL argument.

 

I tried several different things when refreshing the display. Here are a few of my attempts:

 

$OPT=$argv[0];

$OPT=$argv[1];

 

Nothing!

 

Actually, there were several more, but they're not worth mentioning.

 

Inside of this same script, I want to initialize the variable "OPT", making it the argument that I'm trying to pass to it:

 

Then, I'm trying to use conditional statements to check variable value:

 

if ($OPT == 'alt')

{

....

}

 

I also tried a "$_POST[name]", to recover the variable, but it didn't work either.

 

This is not supposed to be an interactive prompt. It should only pass a new variable, when one of the button options is selected.

 

Any help would be greatly appreciated.

 

 

 

Link to comment
Share on other sites

Is it possible to use something similar with the select dropdown function?

 

example:

 

echo '<form action="Get_Old_Assets_By_Alt.php?update=$stat&code=$alt" method="post"> <select name="stat"> <option value="2"> Abandon </option> <option value="3"> On Hold </option> <option value="4"> Provider Input Requested </option> <option value="5"> Working </option> <option value="6"> Complete - Not Yet Delivered </option> <option value="7"> Complete - Delivered </option> </select> <input type="submit" value="Update"></input> </form>';

 

Where the variable "code" could have been set via previously defined variable assignment.

 

If this is possible, this could work!

 

 

Link to comment
Share on other sites

You can set the form's action like that, although those variables won't substitute their values like you wrote the code. You can also make them hidden fields:

 

<form action="Get_Old_Assets_By_Alt.php" method="post">

<input type="hidden" name="update" value="<?php echo $stat;?>">

<input type="hidden" name="code" value="<?php echo $alt;?>">

 

The only difference is that you access the values in $_POST instead of $_GET. Or, you could set the form's method to get and then access everything inside $_GET.

 

I think your efforts with that failed because you were trying to mix Javascript and PHP too much without understanding how they interact (or specifically, how they don't interact). Anything you can do with regular Javascript/HTML you can write with PHP, you just need to remember that the output needs to be valid Javascript/HTML. My applications have things like CSS and Javascript files that are produced by PHP, where PHP will output dynamic Javascript code or dynamic CSS (in addition to HTML, or images, PDFs, Word documents, spreadsheets, etc). You can produce pretty much anything you want with PHP, but if you're producing Javascript and HTML then the output from PHP needs to be valid Javascript and HTML. Your previous efforts seemed to be missing things like quotes inside the Javascript output, or trying to use PHP variables in places that they weren't defined.

Link to comment
Share on other sites

Here's what I entered for my code:

 

?> <td> <center> <form action="Get_Old_Assets_By_Alt.php.20130925" method="post"> <select name="stat"> <option value="2"> Abandon </option> <option value="3"> On Hold </option> <option value="4"> Provider Input Requested </option> <option value="5"> Working </option> <option value="6"> Complete - Not Yet Delivered </option> <option value="7"> Complete - Delivered </option> </select> <input type="hidden" name="update" value="<?php echo $stat;?>"> <input type="hidden" name="code" value="<?php echo $alt;?>"> <input type="submit" value="Update"></input> </form> </center> </td>

<?php

 

Note that the "$code", or $alt value is coming through to the refreshed page.

 

But, the "$update", or $stat value / variable is NOT coming through to the refreshed page.

 

The $update, or $stat value is the one coming from the dropdown menu.

 

If you can correct the above dropdown code, so that the value is initialized correctly, I would be forever in your debt...

 

I would say that I'd give you my first born, but she's already 30, and out on her own... ;-)

Link to comment
Share on other sites

$_POST['update'] is going to contain whatever value was in the $stat variable (assuming it is a scalar value). The value from the dropdown is going to be in $_POST['stat'], because the name of the dropdown is "stat". When the code above runs there is not a variable defined for what the value of the dropdown is going to be. You only figure out what they selected in the dropdown once the form is actually submitted and the data is sent to the server and PHP. If you want to get the value of the dropdown before the form is submitted then you use Javascript for that, not PHP. PHP runs and is finished before the browser even gets the HTML, you can't have a variable in PHP to represent what they are going to pick before they have picked it.

 

HTTP is a stateless protocol, one request/response cycle is all there is. The only way data gets to PHP is through $_GET, which is variables defined in the URL; or $_POST, which is the content submitted in the body of the request; or $_COOKIE, which contains any cookies that were sent with the request. PHP uses the concept of a session also, where you can save data in the $_SESSION array and it will be available on all pages for the current user's browsing session (assuming you start the session in PHP; behind the scenes PHP uses cookies to associate a session on the server with a particular client). Other than through those methods, PHP does not have access to any other data. You can't define a variable in one script and have that same variable be available in another request if you haven't done anything like save it in the session or submit it with a form or whatever else (a discussion about techniques like shared memory and multi-threading is probably out of scope here). It sounds like you were trying to use a variable in PHP to represent the data that they user is going to select from the dropdown, but PHP doesn't work that way. Once the script finishes and the response gets sent to the browser, PHP shuts down and clears everything from memory until the next request. The next request does not start with any data from the previous request other than what is in $_GET, $_POST, and $_COOKIE (plus whatever data it can get from the request HTTP headers sent by the browser).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...