1

I have this category listing

<?php
$var = explode('|', $_POST['categories']);
 ?>
  <div id="cardContainer">
<div class="wrapper">
    <div class="catList">
        <div class="categories">
            <h2>Categories</h2>
            <form method="post">
            <select id="categories" name="categories">
                <option value="">Choose a category</option>
                <option value="1|page1.html">Real Estate</option>
            <option value="2|page2.html">Commercial Auto</option>
                <option value="3|page3.html">Beauty/Salon</option>
                <option value="4">Health</option>
                <option value="5">Construction</option>
                <option value="6">Legal</option>
                <option value="7">Furniture</option>
                <option value="8">Food</option>
            </select><!-- end id="categories" -->
            </form>
        </div><!-- end class="categories" -->
    </div><!-- class="catList" -->
    <!-- START IMAGE LISTINGS -->
        <div class="imgList">
            <?php include($var); ?>
        </div>
    <!-- END IMAGE LISTINGS -->
</div><!-- end class="wrapper" -->
</div><!-- end id="cardContainer" -->

and a <?php include(''); ?> on the bottom. How can I insert th e value of the option chosen to the include(''); in php? I am kind of new with php and any help is appreciated thank you.

Edit: I want to open the page depending on the category seleted trying to use the include(); method. If that's not the best way to go can you please let me know? Thanks in advance.

Andres Rivera
  • 97
  • 2
  • 7
  • you would have to get the variable after the form was submitted and then put the variable in the include file with the rest of the address – NoLiver92 Oct 20 '13 at 08:25
  • 1
    Remeber one thing: PHP is serveside. If you don't tell it the server PHP won't know. PHP can only create a whole site, it can't change parts of the site! – idmean Oct 20 '13 at 08:25
  • You can't, the page must be submitted via `get` or `post` method – ritesh Oct 20 '13 at 08:25

2 Answers2

3

you cant do that directly just by using php

because the <option> to be chosen lies on the client side while php is a server side language

you need a client side scripting language like javascript for handling such events ( like onselect or onchange etc.. )

you need to use AJAX in javascript to serve this purpose

Aditya Vikas Devarapalli
  • 3,186
  • 2
  • 36
  • 54
  • Ok so for example if I had `` and I wanted the `html` page to change depending on the category I would have to use AJAX for it to work? – Andres Rivera Oct 20 '13 at 08:29
  • @AndresRivera: This can also be achieved by restructuring your HTML markup a little bit (if you don't want to use AJAX) -- see my answer below. – Amal Murali Oct 20 '13 at 08:31
  • Oh I thought I could just create pages with the name of the category and just change it that easily. Ja! Thanks I will do some reading about AJAX :) – Andres Rivera Oct 20 '13 at 08:32
1

Nope, you can't do that. The <option> text is not sent to the server and there's no way to retrieve it using PHP alone. You'll have to use AJAX to grab the text and send it to your PHP script.

However, you can achieve it by restructuring your HTML markup a little bit:

<select id="categories">
    <option value="">Choose a category</option>
    <option value="1|Real Estate">Real Estate</option>
    <option value="2|Commercial Auto">Commercial Auto</option>
    ...
</select>

Now, you can simply use explode() to get the text, like so:

$var = explode('|', $_POST['categories']);
echo $var[1]; // echo the text

Demo!


Can it be done without the submit button? to just change when the option is changed?

Yes, but you'll need to use jQuery for that. The following answer should help:

Here's a quick demo making use of the code from the above answer: Fiddle

Community
  • 1
  • 1
Amal Murali
  • 75,622
  • 18
  • 128
  • 150