Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change content depending on a <select>?

Tags:

php

I need help on 2 things since I'm not savy in PHP: Content that changes depending on the:

<select>
    <option>Option 1</option>
    <option>Option 2</option>
</select>

I think this can be done using PHP, by giving names to the options, having PHP get the names, creating variables depending on those names and putting an "include" that shows the changing content.

Here's the model for the script:

READ Select
IF User selects "variable1, varialbe2,etc"
THEN Display "page1,page2,page3"
*The page changes within the page so that if the user wants to change it again they dont have to go back...Maybe this can be done by using:

<?php include 'page1.php'; ?>

That content will include a button, and depending on that button, I want a text box to be filled with certain text.

<input type="button" value="">

Then Depending on the value of that button the PHP will put text into a textbox. From what I've read this will be using $request and $post ... (This of course is separate PHP)

like image 664
elizur424 Avatar asked Aug 31 '25 04:08

elizur424


2 Answers

There may be a couple of different ways to accomplish your goal. If we stick to using PHP as you've requested then something like this is probably best. I've used a case/switch here for security purposes and a POST instead of a GET to prevent URL manipulation although with the case/switch you have addressed a lot of URL manipulation anyway so it becomes a matter of preference for you.

<?php
$page = null;
if(isset($_POST['page'])){
    $page = $_POST['page'];
}
switch($page){
    case 'page3': include_once('/path/to/page3content.php'); break;
    case 'page2': include_once('/path/to/page2content.php'); break;
    case 'page1': include_once('/path/to/page1content.php'); break;
    default: include_once('/path/to/defaultcontent.php'); break;
}
?>
<form name="myform" action="" method="post">
    <select name="page" onchange="this.form.submit()">
        <option value="page1"<?php if($page == "page1"){ echo " selected"; }?>>Page 1</option>
        <option value="page2"<?php if($page == "page2"){ echo " selected"; }?>>Page 2</option>
        <option value="page3"<?php if($page == "page3"){ echo " selected"; }?>>Page 3</option>
    </select>
</form>

However, depending on your file structure, you can accomplish something similar even easer with just a little basic Javascript and HTML.

<select name="page" onchange="window.location=this.value">
    <option value="/path/to/page1.php">Page 1</option>
    <option value="/path/to/page2.php">Page 2</option>
    <option value="/path/to/page3.php">Page 3</option>
</select>
like image 90
Strixy Avatar answered Sep 02 '25 17:09

Strixy


You need to have a name field in your select tag, and value fields in your option tags:

<form name='myform' action='myform.php' method='POST'>
  <select name='myselect'>
    <option value='1'>Option 1</option>
    <option value='2'>Option 2</option>
    <option value='3'>Option 3</option>
  </select>
  <input type='submit'>
</form>

Then the PHP page will receive the selection via $_POST['myselect'].

like image 42
Sammitch Avatar answered Sep 02 '25 19:09

Sammitch