?
Solved

Passing a post variable to a global variable with a "Select" button

Posted on 2012-09-04
21
Medium Priority
?
369 Views
Last Modified: 2012-09-13
I have a page that ask the user to select a "Print Format".  I also have a global variable called "P_Format".  The page basically looks like this:

O Format # 1
O Format # 2
O Format # 3
|Select|

What I would like to do is change the global variable to what ever the user selected and return to the page that called it.  Since this "Print Format" page will be called from multiple other pages, I'm not sure how to do it.  I know how to go back to the calling page, but how do I query the radio buttons and set the global variable when the user presses "Select"?
0
Comment
Question by:breeze351
  • 11
  • 9
21 Comments
 
LVL 45

Expert Comment

by:Chris Stanyon
ID: 38365274
PHP variables only exist as long as the code is running. To maintain a variable that will be accessible throughout your application, you should use sessions. Make sure to add session_start() at the very top of very page.

You can then set the variable like so, in the script that handles your POST request:

//you might want to add some validation before assigning it!
$_SESSION['P_Format'] = $_POST['yourRadioID'];

You can then retrieve it on any page like so:

echo $_SESSION['P_Format']
- or -
$myVar = $_SESSION['P_Format']
0
 

Author Comment

by:breeze351
ID: 38365585
This is not the question.  "P_Format" is already defined and the "session_start()" is at the top of every page.

I need to pass the value of a radio button back to the session variable and then go back to the calling page.
0
 
LVL 45

Expert Comment

by:Chris Stanyon
ID: 38365695
Create a PHP script with the following, and then post your form to it:

<?php 
session_start();
$_SESSION['P_Format'] = $_POST['yourRadioID'];
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38367556
It seems like you might be looking for an AJAX solution?  In any case, you might want to consider doing it with a cookie that survives beyond the instant browser session.  Most people do not change printers or desired print formats very often, and a site that requires me to repeat my preferences over and over again every time I visit would become annoying after the third time it failed to remember my preferences.  Just a thought... ~Ray
0
 

Author Comment

by:breeze351
ID: 38369182
Ray
It's a commercial real estate system. So I'm setting the "Print Format" to the most used at the log in.  The majority of the time they're printing out the listing to give to a client.

I don't know if you've ever worked with real estate brokers, they're not the sharpest knife in the draw.  I had one customer screaming at me because he did a survey for 5,000 to 6,000 square feet and a piece of space didn't come up.  "I know this is in the system!!  Your software sucks!!".  It turns out the girl doing the data entry listed the space as 4,750 square feet.  In real estate, the square footage is determined by who measures it.  The landlord will give you one number, the listing agent gives you another and the building plans a third number.

Chris

Just so I understand this.  

On page 1, I add :
$_SERVER['HTTP_REFERER'] = "Multiple_Spaces.php"
This page calls "Print_Formats.php"

I create a page called "Something.php" with the above code.
When the user clicks select, "Print_Formats.php" calls "Something.php"

"Something.php" will then re-direct back to "Multiple_Spaces.php"

Glenn
0
 
LVL 45

Expert Comment

by:Chris Stanyon
ID: 38369300
you need to just submit your form to the script I posted. That will store your value in the session and then redirect back to the page that called it.

As Ray has already pointed out, you'd be better off posting the value using ajax, as your user never leaves the page, so there's no need to return them. The info is posted to your script 'behind-the-scenes'.
0
 

Author Comment

by:breeze351
ID: 38369328
Okay, I'm lost.
"Multiple_Space.php" calls "Print_Formats.php"
"Print_Formats.php" calls "Something.php"
"Something.php" is going to re-direct back to "Multiple_Space.php"?
0
 
LVL 45

Expert Comment

by:Chris Stanyon
ID: 38369402
OK. I think I've got what you mean :)

In your print_formats page, you need to grab the referrer. You can then pass this along to the 'something' script (please think of a better name!), and use it to redirect. In your print_formats page, add a hidden input to the same form as the radio buttons.

<input type="hidden" name="referrer" value="<?php echo $_SERVER['HTTP_REFERER']; ?> " />

Open in new window

Then in the 'something' script, use this instead:

<?php 
session_start();
$_SESSION['P_Format'] = $_POST['yourRadioID'];
header('Location: ' . $_POST['referrer']);
?>

Open in new window

It's a little crude, but it should get the job done.
0
 

Author Comment

by:breeze351
ID: 38369777
Chris
It does return to the original calling page, however, it's not setting the $_Session variable.

I changed "Print_Formats_Return.php" (I wasn't going to call it "Something.php"!) to read as follows:

<?php
session_start();
$_SESSION['P_Format'] = $_POST['Radio'];
header('Location: ' . $_POST['referrer']);
?>

And I've added the following to "Print_Formats.php":

<input type = "hidden" name = "referrer" value="<?php echo $_SERVER['HTTP_REFERER']; ?> " />

The start  of the code for the table where the radio button is as follows:

while ($count < 4)
      {
            echo '<tr>';
        echo '<td width="150" align = "center">';
            echo '<input name="Radio" type="radio" id="1" value=';
            $work = ($count +1);
            echo $work;
            echo ' ';
            if ($_SESSION['P_Format'] == $work)
                  {
            echo 'checked="checked"';
                  }
            echo '</td>';

Any ideas?

Glenn
0
 
LVL 45

Expert Comment

by:Chris Stanyon
ID: 38369863
OK. Your code is slightly off. An ID is unique - you're setting all yours to 1. Have a look at this code:

for ($count=1; $count<5; $count++):
 	echo '<tr><td width="150" align = "center">';
	echo sprintf('<input name="Radio" type="radio" value="%s" %s />', $count, ($_SESSION['P_Format'] == $count) ? 'checked="checked"' : "");
	echo '</td></tr>';
endfor;

Open in new window

Make sure that is part of the form that contains the referrer hidden input and submits to your Print_Formats_Return script
0
 

Author Comment

by:breeze351
ID: 38373592
Now you've really lost me!  The following code works in another script:

            while ($ROW = Mysql_fetch_assoc($Survey_Data, MYSQL_BOTH))
            {
                  $count = ++$count;
                  echo '<tr style="height: 24px;">';
                  //Select button
                  echo '<td>';
                  echo '<div align="center">';
                  echo '<input name="Radio" type="radio" id=1 value = "';
                  echo $ROW['SEQ'];
                  echo $ROW['STOREKEY'];
                  echo '"';
                  if ($count == 1)
                  {
                        echo 'checked="checked"';
                  }
                  echo ">";
                  echo "<br>";
                  echo "<br>";
                  echo "<br>";
                  echo "</td>";

I have the same ID and I'm just changing the VALUE.

Glennn
0
 
LVL 45

Expert Comment

by:Chris Stanyon
ID: 38373859
You can't have the same ID in a page - an ID has to be unique! If you can't give the radio options a unique ID then leave it off. Your previous code might work, but it's wrong!

Maybe let me know what else you're lost on. The code I posted will generate 4 radio options with values 1 through to 4. If the value matches the $_SESSION['P_Format'] value, then the radio will be checked.
0
 

Author Comment

by:breeze351
ID: 38377442
Why does my previous post work?

I'm setting an "ID" and then a "Value".

If I'm going to a "Post" page, the value should be whatever I clicked.

Glenn
0
 
LVL 45

Expert Comment

by:Chris Stanyon
ID: 38378247
OK. As I said previously, your code might work - but it's wrong!

An ID has absolutely nothing to do with POSTing a form. When you POST a form the Name and Value are passed through to the POST array. A radio list looks something like this:

<input type="radio" name="favouriteCheese" value="Stilton" />
<input type="radio" name="favouriteCheese" value="Cheddar" />
<input type="radio" name="favouriteCheese" value="Yarg" />

Open in new window

When the form is submitted, the post array will contain a Key called 'favouriteCheese' and a value of whatever was selected, for example:

$_POST['favouriteCheese'] = "Stilton"

If you want to attach clickable labels, then you can add an ID to your list, but they MUST BE UNIQUE!

<label for="stilt">Stilton</label>
<input id="stilt" type="radio" name="favouriteCheese" value="Stilton" />
<label for="ched">Cheddar</label>
<input id="ched" type="radio" name="favouriteCheese" value="Cheddar" />
<label for="yarg">Yarg</label>
<input id="yarg" type="radio" name="favouriteCheese" value="Yarg" />

Open in new window

Here's the DOCs for HTML Forms - http://www.w3schools.com/html/html_forms.asp
0
 

Author Comment

by:breeze351
ID: 38379766
Chris:
I'm pulling my hair out on this one!

The web site is "chris.mrbreeze.net"
The log in is "sales" "lc"

Go to "Search Buildings"

The address is "362" "2nd".

Click on "Print Format" and select something.

I'm not worried about opening this site since all the data is test.
The client knows it so there is no problem.

Take a look.

Thanks
Glenn
0
 
LVL 45

Expert Comment

by:Chris Stanyon
ID: 38379999
Hi Glenn,

I've had a look through the code on your site and to be honest it's a bit of a mess. Here's what I can see on the various pages.

Search_Buildings.php
2 inputs - both with an ID of Street - I'm only going to say this one more time - IDs HAVE TO BE UNIQUE. YOU SHOULD NOT HAVE 2 ELEMENTS ON A PAGE WITH THE SAME ID. I can't really make it any clearer than that, so sorry for shouting it out!

The page POSTs the values to a script called BuildingChk.php and then redirects to a page called Multi_Space.php. Because BuildingChk.php is a server side script - I can't see what it does, how it handles the data, or how it redirects to Multi_Space.php

Multi_Space.php
The radio options all have an ID of '1'!

Although the form has it's action set to Space_Display.php, the Print Format button fires a javascript function to simply redirect to the Print_Formats.php page - no data is ever POSTed to the Print_Formats page because you are never Submitting the form.

Once you're on the Print_Formats page, all the inputs have an ID of '1' - you're consistent, I'll give you that :)

The code for the inputs has gotten messed up somewhere in your code. They come out like this:

<input type="radio" td="" <="" checked="checked" value="1" id="1" name="Radio">

The Select button fires a javascript function that is exactly the same as clicking the Back button in your Browser - not really sure why!

Again, no data is ever being POSTed anywhere. The Print_Formats_Return page is never called. Also, the opening <form> tag is inside the HEAD element, and doesn't seem to have a closing tag.

A few other observations I have (which may or may not be welcome)...

Your pages don't use a valid doctype, so they render in Quirks mode. There a a good number of HTML errors (failed validation) and a valid doctype might fix some of these. They really ought to be fixed one way or another.

You seem to be using a 660 line javascript file simply to make your navigation text bold on hover. You can (should) do this with one line of CSS.

Your site is using tables for design. That's a bad idea and you should be using a DIV based layout. This won't really effect the functionality of your site, it's just considered best practice (and much better for search engines, screen readers, design tweaks etc).

It looks like you're using Dreamweaver to create your code. It may seem nice and easy to to use a point and press package, but it creates ugly code, and hinders your learning / understanding of modern web design practices. Use a decent IDE instead - Eclipse, Aptana, even Notepad++ !

I don't mean to be hyper-critical, but your site seems a long way from being functional. It may also sound a little harsh, but I would recommend maybe hiring a professional web designer / developer to do the site. There are really too many issues to resolve in one EE question.

If you really want to carry on with the project yourself, then you really need to understand how forms work and how they pass data to the server. You'll probably want to read up on PHP session handling as well, plus a few of PHP's debugging tools, particularly var_dump and error_reporting. If there's something specific you don't understand or you need help with, then EE is always on hand.
0
 

Author Comment

by:breeze351
ID: 38384054
Chris:

I'm going to your reply one by one.

Search_Building.php

Search_Building.php asks for a # and a street.  They are both not required.

This is the purpose of "Building_Chk.php":

A):  If you leave the # blank and give a street of "2nd", it displays everything on "Second Avenue" by re-directing to "Street _Display.php".  
B):  If you add a building # (14 2nd),  it only shows you that one building.  If there is only one space available in the building it re-directs you to "Space_Display.php".
C):  If there is more than one space available at "362 2nd", it re-directs you "Multi_Space.php"

Also, "Building_Chk.php" checks the street name.  In Manhattan, "2nd" is also "Second" and "6th", "Sixth" and "Avenue Of The "Americas" are the same.

I did see what you were saying about the "ID" in "Search_Building.php".  But why does it work? Try it yourself.

A):  Street = "2nd"
B):  # = "14"  Street = "2nd"
C):  # = "362"  Street = "2nd"

Also the select buttons in "Street_Display.php" and "Multi_Display" work and re-direct to "Space_Display.php".

I'll get back to you in a day or two about the others.

Thanks
Glenn
0
 

Author Comment

by:breeze351
ID: 38384131
Chris:

This is weird!

As you observed, the code in "Search_Building.php" is:
         <td colspan = "2">
          <div id="Body_Lables">
          <input name="Address" type="text" id="Street" value="" autocomplete="Off"></td>
           </div>      
         </td>

            <td colspan="2">
         <div id = "Body_Labels">
          <p style="margin-bottom: 0px;">Street Name:</p>
         </td>
         <td colspan = "4">
         <div align="left"><input name="Street" type="text" id="Street" value="" autocomplete="Off">

But the code in "BuildingChk.php" is:
// Get passed variables

$Address = $_POST['Address'];
$Address = strtoupper($Address);
$Street = $_POST['Street'];
$Street = strtoupper($Street);

Where is it getting the value for address # from?

And the code works!

Are you sure that it's not pulling the value from the input name?


Glenn
0
 
LVL 45

Accepted Solution

by:
Chris Stanyon earned 2000 total points
ID: 38384295
The ID has nothing to do with posting information. When you post a form, the name of the input, along with it's value get POSTed. The following HTML:

<input name="Address" type="text" value="">
<input name="Street" type="text" value="">

will give you:

$_POST['Address']
$_POST['Street']

Your code works because the ID isn't used for POSTing the info. It's more of a CSS selector for layout (and DOM manipulation)

Regarding the other bits. Re-directing to a page is not the same as POSTing to a page. Your page only has access to a POST array (form values) if a form was SUBMITed to it. Therefore Space_Display and Multi_Display don't have access to the POST array, because the the form data from Search_building was POSTed to Building_Chk.

The Print Formats button doesn't POST the information to the Print_Formats page. It just simply visits that page, so your print_formats page has absolutely NO knowledge of what was selected on the Multi_Space page.

The Display button on the Multi_Space does work because it SUBMITs the information in the form to it's action page - Space_Display.php

The Select button on the Print Formats page doesn't work, because clicking it is EXACTLY the same as clicking the Back button in your browser, and as such, no information is ever POSTed anywhere.
0
 

Author Comment

by:breeze351
ID: 38396129
Chris:
I know I have to clean up the code, but I'm working on 3 things at the same time and they are not all php.  I also have code in Delphi and PHP.  

This customer just wants something to look pretty right now and that what I'm trying to do right now.  Once we get some cash I can go back and clean it up so I can sell it to someone else.

I figured out the problem.  It actually was Filezilla dropping the connection.  I wasn't paying attention, so what I thought was transferred wasn't !  I kept staring at the code and I finally sorted the code by date in Filezilla on the server.  "Hey wait a minute, I just modified that yesterday!"

Thanks for your help.
Glenn
0
 

Author Closing Comment

by:breeze351
ID: 38396148
It was my fault.  FileZilla was dropping the connection and I never noticed.  So what I thought I was sending never ended up on the server.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question