user3776900
user3776900

Reputation: 29

Results on the same page as the search form php

How can i echo the results in the same page as the search form is (wordpress)?(i`m newbie)

   <form action="" id="searchform" method="POST">
<input name="searchquery" type="text" placeholder="Nume, spital" size="30" maxlength="40">
        <select name="filter">
            <option value="ALL">Toate judetele</option>
            <option value="AB">Alba</option>    
        </select>
    <input type="submit" value="Cauta">
     </form>

the php code

 global $wpdb;

$con=mysqli_connect("localhost","root","");

$search = mysql_real_escape_string(trim($_POST['searchquery']));
if($_POST['filter'] == "ALL"){
$sqlCommand = $wpdb->get_results("SELECT * FROM `wp_doctors` WHERE `name` LIKE '%searchquery%'");
} else if($_POST['filter'] == "AB"){ $sqlCommand = $wpdb->get_results("SELECT * FROM `wp_doctors` WHERE `name` LIKE '%searchquery%' AND `city`= 'Alba'   ");
} //more if`s, i just cuted for example

foreach ( $sqlCommand as $result) 
{
    echo $result->id;
    echo $result->name;
    echo $result->spec;
}

and if i`m using at the form action atribute it will take me to the index page. Thanks!

Upvotes: 0

Views: 1424

Answers (1)

Hidran
Hidran

Reputation: 328

Try putting in the form action :

action="<?=$_SERVER['PHP_SELF']?>" 

In order to test if you are not being redirected by wordpress, try this in your php code:

if(isset($_POST)){
 var_dump($_POST);
}

In this way you can clear out if the page is posting right and if you are being redirected somewhere by wordpress.

Upvotes: 1

Related Questions