Results on the same page as the search form php - 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!

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.

Related

Using form method="post" to assign a php variable dynamically

I'm trying to run a specific database query based on what value is selected in a select drop down. The current code I have isn't running the queries.
My current code looks like this:
main-search.php
<form action="" method="post">
<div class="search-container">
<input type="text" id="search_input" placeholder="">
<select name="search-by">
<option value="symptom">Symptom</option>
<option value="language">Language</option>
</select>
</div>
</form>
<?php
session_start();
$searchBy = $_POST['search-by'];
$_SESSION['search_type'] = $searchBy;
?>
search.php
<?php
session_start();
$queryType = $_SESSION['search_type'];
if ($queryType == "symptom") {
$searchQuery = $db->query("SELECT * FROM Symptoms WHERE symptom_name ORDER BY symptom_name ASC");
}
else if ($queryType == "language") {
$searchQuery = $db->query("SELECT * FROM `Language` WHERE language_name ORDER BY language_name ASC");
}
?>
I have tried changing the code in main-search.php to...
<?php
session_start();
$searchBy = $_POST['search-by'];
$_SESSION['search_type'] = "symptom";
?>
and it works as intended, running the first query, so that's why I'm assuming I'm doing something wrong with the post form and assigning the $searchBy value.
I'm new to php, so any help would be appreciated, thanks.
It's probably best to avoid Session variables for this purpose, as session variables are used to store/pass information from page to page in PHP (such as login status).
From my deduction you are trying to change this PHP variable dynamically when the user changes the <select> option. As PHP is a server-side script this will not work, if you need something to change without reloading the page you will need to use a client-side script (e.g. Javascript/jQuery). For example (in jQuery):
$('select[id="search-type"]').on("change", function(){
//Do work
});
However, if I understand you correctly, your best bet is to just use HTTP POST variables (accessed by $_POST in PHP), for example:
main-search.php:
<form action="search.php" method="post">
<div class="search-container">
<input type="text" id="search_input" placeholder="">
<select name="search_type" id="search-type">
<option value="symptom">Symptom</option>
<option value="language">Language</option>
</select>
<button type="submit">Submit</button>
</div>
</form>
search.php:
<?php
$queryType = $_POST['search_type'];
if ($queryType == "symptom") {
$searchQuery = $db->query("SELECT * FROM Symptoms WHERE symptom_name ORDER BY symptom_name ASC");
}
else if ($queryType == "language") {
$searchQuery = $db->query("SELECT * FROM `Language` WHERE language_name ORDER BY language_name ASC");
}
?>

Submit form redirect variable

I'm trying get a form to redirect to a certain page depending on what option is chosen in a select div. When I press submit, it redirects to the home page. I realize it's because it doesn't change $industry_choice until after the I press the button... is there a method to change this the moment you select a value?
Let's say I choose banking, and $industry_choice = "banking".
if(empty($_POST['industry'])) {
$industry_choice = "";
} else {
$industry_choice = $_POST['industry'];
}
<form method="post" action="http://localhost:8888/wordpress/<?php echo $industry_choice ?>">
<select name="industry">
(lots of options here)
</select>
<input type="submit" class="select-industry" value="Select">
</form>
You are doing it wrong. I think you are not clear with the action attibute of the form.
<?php
if(empty($_POST['industry'])) {
$industry_choice = "";
} else {
$industry_choice = $_POST['industry'];
header('Location: http://localhost:8888/wordpress/'.$industry_choice);
}
?>
<form method="post" action="">
<select name="industry">
(options)
</select>
<input type="submit" class="select-industry" value="Select">
</form>
action attribute will redirect to the page you processed the input. After that you will redirect it to targeted location.

Show echo inside a DIV

I'm having a very annoying issue and I have no idea what I'm doing wrong. It has (again) something to do with loading in a page into a DIV.
I have made a form which can be used to update information into a database. This form and PHP code is in one file and is being loaded in one DIV. When I visit the page which is loaded into the DIV itself, everything is working fine and the database is being updated as it should.
Though, when the page is loaded into the page. When I press submit nothing happens. What I want to happen is that the echo, which is either "Success!" or "Error!" is being displayed in the same DIV as the page is loaded into. This is my code, I hope someone can help! some variables are in Dutch, excuse me for that.
if(isset($_POST['submit'])) {
include "database.php";
session_start();
$id = $_POST['id'];
$titel = $_POST['titel'];
$text = $_POST['text'];
$categorie = $_POST['categorie'];
$auteur = $_SESSION['sess_loginnaam'];
$laatst_aangepast = date("Y-m-d H:i:s");
$sql="UPDATE paginas SET id='$id', titel='$titel', text='$text', categorie='$categorie', auteur='$auteur', laatst_aangepast='$laatst_aangepast' WHERE id='$id'";
$result=mysql_query($sql);
if($result){
echo "Success!";
?>
<META HTTP-EQUIV="refresh" content="2;URL=index.php">
<?php }
else {
echo "Mislukt!";
}
}
else {
include "database.php";
$id = $_GET['id'];
$sql="SELECT * FROM paginas WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
<form name="form1" method="post" action="">
ID:
<input name="id" type="text" id="id" value="<? echo $rows['ID']; ?>" size="2"></div> Titel:
<input name="titel" type="text" id="titel" value="<? echo $rows['titel']; ?>" size="50%"> Categorie: <select name="categorie"><option value="Paginas">Pagina's</option>
</select>
Tekst:
<textarea name="text" type="text" id="text" rows="31" cols="79"><? echo $rows['text']; ? ></textarea>
<button type="submit" name="submit">Edit!</button>
</form>
And here is the code I use to load this page into the DIV:
$("#edit").on('click',function(){
$('#content').load($(this).attr('href'));
});
So how can I manage to display the echo into the same DIV? :)
$.load is equivalent to $.get whereas your php code detect $_POST, that's way there is no response.
You can change your js code to
$("#edit").on('click',function(){
$.post($(this).attr('href'), YOUR_FORM_DATA_HERE, function(data){
//update your page here with response
});
});
See $.load - jQuery Doc

refresh the page without redirecting

I have a search form like this in a file form.php. It works great, the problem I am facing is that I use class/methods(functions). For example I have this method that calls logo and manu bar.
//$this->output is just a method that echos, that's all it does
function header()
{
$this->output('<DIV CLASS="header">');
$this->logo();
include('form.php');
$this->links();
$this->header_clear();
$this->output('</DIV>');
}
Like the above, the form shows perfectly, but when I choose a category it takes me to form.php, and it ruins everything. (form.php is just a form)
I know the problem is here
onChange="javascript:this.form.action='form.php';
this.form.submit()
Without javascript I couldn't get the category values, the page is not refreshing, so $_POST['cat'] is empty.
What is a better way to refresh the page without taking me to different one?
I know probably this all confusing, just bear with me.
<form enctype="multipart/form-data" name="form" id="form" action="form.php"
method="get">
<SELECT name="cat" onChange="javascript:this.form.action='form.php';
this.form.submit()">
<option value="" >Categories</option>
<?php
$z = "0" ;
//Select categories form database
$category =db_select_categories($z);
if(is_array($category))
{
foreach($category as $rowc)
{
$id_cat = htmlentities($rowc['id_cat'], ENT_QUOTES);
$cat_name = htmlentities($rowc['cat_name'], ENT_QUOTES, 'utf-8');
$cat_name = stripslashes($cat_name);
echo "<option style=\"background-color:#FFCC99\" disabled>".$cat_name."
</option>";
$categories = db_select_categories($id_cat);
if(is_array($categories))
{
foreach($categories as $row)
{
$id = htmlentities($row['id_cat'], ENT_QUOTES);
$name = htmlentities($row['cat_name'], ENT_QUOTES, 'utf-8');
$name = stripslashes($name);
if ( $cat == $id )
{
echo "<option value='".$id."'selected> ".$name."</option>";
}
else
{
echo "<option value=".$id."> ".$name."</option>";
}
}
}
}
}
echo "</SELECT>";
?>
<div>
<?php
if(!empty($cat))
{
//Outputs a populated select option depending on category
display_search_options($cat);
}
?>
</div>
If you don't want it to go to form, just make it go somewhere else...
onChange="javascript:this.form.action='index.php';
this.form.submit()

PHP Passing value

Im having trouble passing the value id in my code to edit.php.
In displaynews i print out articles from the database. It also creates a link that redirects me to edit.php and sending the $id value with it.
Link to displaynews function
http://snipt.org/zhla8
Here's where im having trouble
<h3>EDIT NEWS ARTICLE</h3>
<form id="EditNews" name="EditNews" method="POST" action="edit.php">
<textarea name="editnewstext"><?php $news=Textarea(1);echo $news ?></textarea> <!--HERE i need to replace the 1 with id passing in displaynews -->
<input type="submit" name="Edit_News" id="Edit_News">
<?php
include 'db.php';
include'editnewsarticle.php';
if(isset($_POST['Edit_News']))
{
$content= $_POST['editnewstext'];
geteditnews(1,$content); //<!--HERE i need to replace the 1 with idpassing in displaynews -->
Header("location:Home.php");
}
Link to Edit.php page
http://snipt.org/zhkj8
Link to GetnewsTextarea function
http://snipt.org/zhlb9
Link To editnewsarticle function
http://snipt.org/zhki2
Please dont comment on how the mysql extension is depreciated and that my code is open for sql injections.
Thanks in advance
EDIT: Here's the solution
if(isset($_GET['id']))
{
$id = $_GET['id'];
$data = mysql_query("SELECT * FROM news WHERE id = '$id' ");
}
?>
Does these changes in edit.php help?
if (isset($_POST['id']))
$id = $_POST['id'];
<?php $news=Textarea($id);echo $news ?>
geteditnews($id, $content);
Add a hidden field for the id right after the form tag. As such:
<form id="EditNews" name="EditNews" method="POST" action="edit.php">
<input type="hidden" name="id" value="<?php echo $id; ?>">

Categories