0

This is my current URL.

I've had this new project now for some time and I was wondering if:

  1. There is a way that when the user clicks Gadgets it sends him to .... cat=gadgets, and it also remembers the previous selections
  2. How can I transform that awful looking dropdown list into something more appealing like radio or just two basic buttons.

My code:

<div id="advanced-search">
    <form action="<?php bloginfo('url');?>/recipes" method="GET" id="searchF1">
        <?php
        $searched_term = get_query_var('recipe_search');
        if (empty($searched_term)) {
            $searched_term = isset($_GET["search"]) ? $_GET["search"] : "";
        }
        ?>
        <input id="sfield" type="text" name="search" placeholder="keywords" <?php if (!empty($searched_term)) {echo 'value="'.$searched_term.'"';} ?>>
        <select id="img" name="images">
            <option value="1" <?php if($_GET["images"]=='1'){ echo 'selected="selected"';} ?>>with pictures</option>
            <option value="0" <?php if($_GET["images"]=='0'){ echo 'selected="selected"';} ?>>without pictures</option>
        </select>
        <div id="time-side">
            <!--<small>Published time</small>-->
            <input type="text" id="from-side" name="from" placeholder="Start date">
            <input type="text" id="to-side" name="to" placeholder="End date">
        </div>
        <input type="submit" value="Tech">
        <input type="submit" value="Gadgets">
    </form>
</div>
Stacked
  • 6,892
  • 7
  • 57
  • 73
user3026270
  • 187
  • 1
  • 2
  • 9
  • It looks like your page is vulneragle to `SQL Injection`. – magic-sudo Nov 13 '16 at 11:03
  • You can store previous category in session – magic-sudo Nov 13 '16 at 11:04
  • And how exactly can I store them? – user3026270 Nov 13 '16 at 11:18
  • to store data on the client rather than cookies you need to use sessions, take a look here http://www.w3schools.com/html/html5_webstorage.asp and here http://stackoverflow.com/questions/1981673/persist-javascript-variables-across-pages/1981706#1981706 – Accountant م Nov 13 '16 at 11:35
  • Thanks for the info, but I still don't get how can I send the user to a custom url based on what he picked cause I've tried and it didn't work. My question is what exactly do i need to change n my code in order to make that work? – user3026270 Nov 13 '16 at 11:49

2 Answers2

0

You can save data over multiple pages in a few different ways.

  1. url: You can transfer data through url's, like this: example.com/index.php?id=12345 You can then get the id through $_GET, like this $_GET['id']
  2. session You can save a value in a session, probably your best option for the saving of the previous data. You can save a value like this: $_SESSION["key"] = "value"; then you can acces that value anywhere on your website like this: $_SESSION["key"]. Very nice and simple.

  3. cookies You can store a cookie just like you can with $_SESSION but now it's saved on the users computer as long as you want. Please read the manual if you want to use it.

So in your case, you can send your catagory information through the url. Like this: example.com/file.php?catagory=toyota. To get that data in your php code you can do it like this: $_GET['catagory']

The storing of the data of your previous section

$_SESSION["previoussection"] = "some value";

You can then later acces it like this: $_SESSION["previoussection"] and you will get "some value"

Nytrix
  • 1,139
  • 11
  • 23
0

Try this:

<?php
 session_start();//starting session
$searched_term = isset($_GET["search"]) ? filter_var($_GET["search"],FILTER_SANITIZE_FULL_SPECIAL_CHARS) : "";
if(!empty($search_term)){

    $_SESSION = array();//emptying session for putting new data
    $_SESSION['previous'] = $search_term;//add the new search term to the session with previous key
}else{

    if(!empty($_SESSION['previous'])){//if session is not empty
        $search_term = $_SESSION['previous'];//put the old data to the search term variable
    }   
}

?>
    <div id="advanced-search">
       <form action="recipes" method="GET" id="searchF1">                
       <input id="sfield" type="text" name="search" placeholder="keywords" <?php if (!empty($searched_term)) {
      echo 'value="'.$searched_term.'"';} ?>>

        <input type="radio" name="images" value="1" <?php if(isset($_GET["images"]) && $_GET["images"]=='1'){ 
                echo 'Checked';} ?>>with pictures</option>
          <input type="radio" name="images" value="0" <?php if(isset($_GET["images"]) && $_GET["images"]=='0'){ 
                echo 'checked';} ?>>without pictures</option>
        </select>
         <div id="time-side">
              <input type="text" id="from-side" name="from" placeholder="Start date">
              <input type="text" id="to-side" name="to" placeholder="End date">
          </div>
           <input type="submit" name="Tech" value="Tech">
           <input type="submit" name="cat" value="Gadgets">
          </form>
     </div>

You have two button Tech and Gadgets, so in order to send the way you in the url, you need name value pair:

       <input type="submit" name="Tech" value="Tech">
       <input type="submit" name="cat" value="Gadgets">
Mawia HL
  • 3,605
  • 1
  • 25
  • 46
  • Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /var/www/clients/client1/web2/web/wp-includes/formatting.php:5011 – user3026270 Nov 13 '16 at 12:30
  • I made the functions work, but on my sidebar it keeps showing the warning. Is there anyway I can make them dissapear? – user3026270 Nov 13 '16 at 12:38
  • Put session start at the beginning of your script. – Mawia HL Nov 13 '16 at 12:38
  • It's at the beggining of the file, but the warning keeps showing. Is there a way to supress it? – user3026270 Nov 13 '16 at 12:42
  • Did you use session_start() in other file which you might happen to include in this file. Or you may include file which has session_start(), in that case you won't need use sesstion_start at all. You can also hide the error by putting `error_reporting(0);` at the beginning of the script. – Mawia HL Nov 13 '16 at 12:56
  • Thank you so much, do you know how can I transform select into 2 radio buttons insted of the ugly list? – user3026270 Nov 13 '16 at 13:07
  • This is what I've done so far, but it doesn;t even display the radio buttons. with pictures without pictures – user3026270 Nov 13 '16 at 13:10
  • 1
    Try this: >with pictures >without pictures – Mawia HL Nov 13 '16 at 13:11
  • Thank you so so so so so so so much, you are awesome – user3026270 Nov 13 '16 at 13:13
  • It is just a waste of time helping you if you don't click the tick icon. – Mawia HL Jun 17 '18 at 07:31