Hacer algo al cargar la página, en función de una acción de página anterior

I am building a site on wordpress and I have encountered a problem that I am not quite sure how to solve.

La situación:

  1. User makes a selection from a <select> tag on one page and submits their choice
  2. User arrives at a page featuring the same <select>. When the user selects an option, it displays content corresponding with that option using a little bit of jQuery.



$('#servicearea').change(function() {

  var optionValue = $ (this).attr('value');


El problema:

  1. I need the selection from the first page to carry over to the second page then run this script.


  1. I'll be honest, I don't know where to start with this, maybe cookies? I was hoping there was a jQuery type of solution as I am sort of comfortable with jQuery.

Cualquier sugerencia sera apreciada.

preguntado el 03 de mayo de 12 a las 20:05

either sessions or a hidden form field. -

hidden form fields sound like they're close to my capacity, can you elaborate? -

in your form on page 2 <input type="hidden" name="foo" value="POSTED FROM PREVIOUS PAGE"> then on page 3 you will have foo, along with the other form values -

2 Respuestas

javascript doesnt know what you have posted to the page.

you can put the value into the javascript from php with something like this:


or you can put it into a hidden form field

<input type="hidden" id="hiddenField" value="<?=$_POST['selected']?>">

and catch the value with


contestado el 03 de mayo de 12 a las 20:05

Alright, sorry if this seems really novice. On the first page. How to I set the action to 'post' and also set it to navigation to a new page. (before the action was the link to the new page) - Patricio

Hidden form fields will work. But if you decide to go with PHP sessions instead, echa un vistazo aquí.

Desde el enlace de arriba ...

// page1.php


echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';

After viewing page1.php, the second page page2.php will contain the session data. Read the session reference para obtener información sobre propagating session ids as it, for example, explains what the constant SID is all about.

// page2.php


echo 'Welcome to page #2<br />';

echo $_SESSION['favcolor']; // green
echo $_SESSION['animal'];   // cat
echo date('Y m d H:i:s', $_SESSION['time']);

// You may want to use SID here, like we did in page1.php
echo '<br /><a href="page1.php">page 1</a>';


Judging by your snippet, I believe you would set the session variable by doing something like this:

<form action="" method="post">
<select type="select" name="servicearea" >
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
    <option value="option3">Option 3</option>
<input type="submit" name="Submit" value="Submit!" />

if (isset($_POST['Submit'])) {
    $_SESSION['optionValue'] = $_POST['servicearea'];
echo $_SESSION['optionValue'];

contestado el 03 de mayo de 12 a las 23:05

This looks like it could work, how do I set up the session variables to grab the html select options? - Patricio

Should be pretty simple. I've added an update to the bottom of my answer with an example. Note that $_POST['servicearea']; will be referring to an input element in your form with the name attribute servicearea (not an id attribute like in your snippet above) - cody bonney

First off, thanks so much! Second: Would I need to set up a seperate js function to handle navigating to page two, or do I do some kind of redirect with php? - Patricio

Sorry I dont think this way is going to work for me. There is session conflicts with wordpress. Thanks anyways. - Patricio

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.