Jump to content

Reload page when choose a value from option list

- - - - -

  • Please log in to reply
7 replies to this topic

#1
charsta

charsta

    Newbie

  • Members
  • PipPip
  • 28 posts
Hello.

I have a drop down menu with a few values and I want to make the page reload when a value has been selected BUT I also want that value to appear on the address link!
The problem is that, the address link, is already passing a value (from a previous page) and I can't make that value stay on the link after the reload...

An example of the address:
website/test.php?movieid=35


This is my code for the option list:

<select name="showing_date" onchange="window.location.href = 'test.php?date='+ this.options [this.options.selectedIndex].value +'&movieid=' + '<?php echo $movieid; ?>' ;">

   <?php

       $date = $result2;

       while($date <= $result3) {

       echo "<OPTION VALUE=\"".$date."\">".$date."</OPTION>";

       $date = date('Y-m-d', strtotime(date("Y-m-d", strtotime($date)) . " +1 day"));

       }

   ?>

</select>

The values in the drop down menu appear correctly.

Now, after I select a value from the option list, I want that value to appear on the link. (the drop down menu has dates)
For example if I choose the date 2012-02-10 I want the link to be like that:
website/test.php?movieid=35&date=2012-02-10

I'm doing something wrong and I can't make BOTH of the values stay at the link!

If someone can help me it will be great!
Thank you!

#2
mikenco

mikenco

    Newbie

  • Members
  • Pip
  • 5 posts
Surely, if you have already assigned the value "movieid" to a $_POST variable from the first page, then you have to assign the second page's variable as something like "movieid2".. They can't have the same name..

Either that, or include a 'hidden field' in the second page containing the values:-
<input name="movieid2" type="hidden" value="<?php echo $_GET['movieid']; ?>" />


So that the first one becomes 'movieid2' and the new becomes 'movieid'..

?

---------- Post added at 03:02 AM ---------- Previous post was at 02:58 AM ----------

Ah, you need concatenate the first to the second!

---------- Post added at 03:07 AM ---------- Previous post was at 03:02 AM ----------

movieid=35 now becomes $_GET['movieid'] (which is currently set to '35') - You can use
 echo $_GET['movieid'] 
where you need it.

---------- Post added at 03:18 AM ---------- Previous post was at 03:07 AM ----------

Try this..



<select name="showing_date" onchange="window.location.href = 'test.php?<?php echo $_GET['movieid']."."; ?>date='+ this.options [this.options.selectedIndex].value +'&movieid=' + '<?php echo $movieid; ?>' ;"> 

   <?php 

       $date = $result2; 

       while($date <= $result3) { 

       echo "<OPTION VALUE=\"".$date."\">".$date."</OPTION>"; 

       $date = date('Y-m-d', strtotime(date("Y-m-d", strtotime($date)) . " +1 day")); 

       } 

   ?> 

</select>




#3
charsta

charsta

    Newbie

  • Members
  • PipPip
  • 28 posts
During the first load of the page:
website/test.php?movieid=35
i used this line of code to achieve the 'movieid' from the line:
$movie_id = $_GET['movieid'];

BUT here:
<select name="showing_date" onchange="window.location.href = 'test.php?date='+ this.options [this.options.selectedIndex].value +'&movieid=' + '<?php echo $movieid; ?>' ;"> 
i was using 'movieid' !!
I changed it to movie_id and now the link changes correctly!
You just helped me solve this! (I was trying to figure out what was the problem for over one hour!!) Thank you!

So the revised code is this:

<select name="showing_date" onchange="window.location.href = 'test.php?movieid=' + '<?php echo $movie_id; ?>' + '&date='+ this.options [this.options.selectedIndex].value  ;">

   <?php

       $date = $result2;

       while($date <= $result3) {

       echo "<OPTION VALUE=\"".$date."\">".$date."</OPTION>";

       $date = date('Y-m-d', strtotime(date("Y-m-d", strtotime($date)) . " +1 day"));

       }

   ?>

</select>


Now I want the selected value to stay selected after the reload of the page.
Is that possible?
Thank you once more mikenco that helped me saw what the problem was!!

#4
mikenco

mikenco

    Newbie

  • Members
  • Pip
  • 5 posts
In the database update forms that I use, I have to echo that a certain product is true or false.. i.e.


	if($this==$that) {	

	$showthis= "selected=' selected'";

	} else {

	$showthis=""; //show nothing

	}


 echo "<option value='YOURVALUE'>HELLO ". $showthis."</option>\r\n"; 

Make sense? So in the code, you have to know if the item is selected or not before you can tell the drop down.

Sorry, I'm not much of a teacher :(

Ultimately, we are trying to establish is an object is true or not and simply echoing 'selected' to the form's drop down.

#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
Much easier to hide all parameters in hidden (or visible if you wish ofcourse) inputs and do a form.submit() on change, then you do it a better way in my opinion.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#6
charsta

charsta

    Newbie

  • Members
  • PipPip
  • 28 posts

mikenco said:

In the database update forms that I use, I have to echo that a certain product is true or false.. i.e.


	if($this==$that) {	

	$showthis= "selected=' selected'";

	} else {

	$showthis=""; //show nothing

	}


 echo "<option value='YOURVALUE'>HELLO ". $showthis."</option>\r\n"; 

Make sense? So in the code, you have to know if the item is selected or not before you can tell the drop down.

Sorry, I'm not much of a teacher :(

Ultimately, we are trying to establish is an object is true or not and simply echoing 'selected' to the form's drop down.

I tried to to something similar to your code. This is what I have done:

<select name="showing_date" onchange="window.location.href = 'test.php?movieid=' + '<?php echo $movie_id; ?>' + '&date='+ this.options [this.options.selectedIndex].value  ;">

   <?php                                                

      if (isset ($_GET['date'])) {

         $choice = $_GET['date'];

         $date = $result2;

         while($date <= $result3) {

            if ($choice==$date) {

               $selected = "selected";

            } else {

               $selected = "";

            }

            echo "<option value=$date selected='$selected'> $date </option>" ;

            $date = date('Y-m-d', strtotime(date("Y-m-d", strtotime($date)) . " +1 day"));

         }

      } else {

         $date = $result2;

         while($date <= $result3) {

            echo "<option value=$date> $date </option>" ;

            $date = date('Y-m-d', strtotime(date("Y-m-d", strtotime($date)) . " +1 day"));

         }  

      }

   ?>

</select>

Maybe it's a little bit confusing or have unnecessary loops but it's the only one that I almost have it working right.
When I choose a date, the page reload correct and the date value appears on the link "&date=2012-01-10" .
But in the option list instead of showing the selected date it shows the last date value (before the selection the selected/showing value was the first date value). In the source code of the page the selected value appears like this:

<option value=2012-01-10 selected='selected'> 2012-01-10 </option>


when all the other values appear like this:

<option value=2012-01-07 selected=''> 2012-01-07 </option>





Orjan said:

Much easier to hide all parameters in hidden (or visible if you wish ofcourse) inputs and do a form.submit() on change, then you do it a better way in my opinion.

I don't really know what do you suggesting me here because I'm new to the world of php and web developing.
But if you mean to keep the values hidden from the link, I prefer for now to be visible. This way I can know whenever on not the values are passing through pages. Probably I will need that hidden option later (if this is what you mean!!)

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
hidden in this case means that they won't show on the web page in clear text, but they are there in the url and in the sourcecode of the page.

example:
First time, you show this:
<form action="form.php" method="GET">
  <input type="text" name="first_field" value="opera">
</form>
that means you get the url form.php?first_field=opera (if the user haven't changed the value from standard)

on next load, you take the variable and re-echo it to an hidden form input:

<form action="form.php" method="GET">
  <input type="hidden" name="first_field" value="<?php echo $_GET['first_value']; ?>">
  <input type="text" name="second_field" value="Sydney">
</form>

here, only the second input is editable, but as you see, the first is visible in the code of the html. if the user don't change this input either, the url will be form.php?first_field=opera&second_field=Sydney


this way, you can pre-load values into a form that a user can't modify, for example for editing a address from his profile, you add his id as a reference uneditable but there so you can find it in the database etc.

now, this isn't the safest way to do things, but in the start, this is a very good way of learning how it works.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
charsta

charsta

    Newbie

  • Members
  • PipPip
  • 28 posts
I found what my problem was.
This the revised code. With this, when I choose a date from the option list the page reloads, the value of the date, appears in the address link and the chosen date is selected in the option list:


<select name="showing_date" onchange="window.location.href = 'test.php?movieid=' + '<?php echo $movie_id; ?>' + '&date='+ this.options [this.options.selectedIndex].value  ;">

   <?php                                                

      if (isset ($_GET['date'])) {

         $choice = $_GET['date'];

         $date = $result2;

         while($date <= $result3) {

            if ($choice==$date) {

               $selected = "selected='selected'";

            } else {

               $selected = "";

            }

            echo "<option value=$date $selected> $date </option>" ;

            $date = date('Y-m-d', strtotime(date("Y-m-d", strtotime($date)) . " +1 day"));

         }

      } else {

         $date = $result2;

         while($date <= $result3) {

            echo "<option value=$date> $date </option>" ;

            $date = date('Y-m-d', strtotime(date("Y-m-d", strtotime($date)) . " +1 day"));

         }  

      }

   ?>

</select>


Thank you all for the help and for your advises!!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users