Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

Linking PHP files

I have 2 files;

- index.php
- search.php

I use a form action on my index.php to submit my search to the file named 'search.php'.

How can I return my results directly back into index.php?

ie - show the results below my search button &box (i have my results being returned on the search.php file as 'search.php?search=Food'

I hope this is clear enough, if not I will try and explain again 🤗
hey ,
so your search.php file generates the results you want to implement below your search button in index.php right ?

now there are several ways to solve your problem ,

first and recommended one would be to use jquery and load the contents . this would work even without refreshing the page.

second one would be to use an isset or isempty in a global if in your index page to determine if you need to display the form or process the search results . you would not need search.php anymore.

please correct me if I missunderstood your question and share some code to better understanding your problem.
If you mean like what Google, YouTube and Facebook offer you, then you need to implement AJAX.
You could try using a get on the index, returning the search results instead of the index page...
Are you guys actually serious.. You don't need AJAX. AJAX just makes it so you don't have to refresh the page.

You can use include() for a really basic approach. If you post your code for index.php and search.php here I can show you how to do it but the basic principle is this:

Code
<html>
[b]<!--[/b] index.php -->

<form name="search" method=POST action="search.php">
<input type="text" name="query"> <input type="submit" name="submit">
</form>
<br />
<?php include('search.php'); ?>
</html>

Code
<?php
// search.php
// this code should print results in plain text if it doesn't already
// don't include a layout in search.php or it'll break the one on index.php

if (isset($_POST['submit'])) {
    if (isset($_POST['query'])) {
      echo "You searched for: " . $_POST['query'];
    } else {
      echo "You didn't enter a search term. Wanna try again?";
    }
}
?>
(Mar 11, 2015 10:05 PM)Zamurick Wrote:  Are you guys actually serious.. You don't need AJAX. AJAX just makes it so you don't have to refresh the page.

You can use include() for a really basic approach. If you post your code for index.php and search.php here I can show you how to do it but the basic principle is this:

Code
<html>
[b]<!--[/b] index.php -->

<form name="search" method=POST action="search.php">
<input type="text" name="query"> <input type="submit" name="submit">
</form>
<br />
<?php include('search.php'); ?>
</html>

Code
<?php
// search.php
// this code should print results in plain text if it doesn't already
// don't include a layout in search.php or it'll break the one on index.php

if (isset($_POST['submit'])) {
    if (isset($_POST['query'])) {
      echo "You searched for: " . $_POST['query'];
    } else {
      echo "You didn't enter a search term. Wanna try again?";
    }
}
?>
I believe this would still go to search.php...
You'd have to remove the action, then it will make a postback and should work
You're right.

Remove the action="search.php" from the <form> tag and it works though. Just tested it.

Also should add $_POST['query'] != '' to the if in search.php or change it to !empty.

Try this:

PHP Code

<?php
// search.php
// this code should print results in plain text if it doesn't already
// don't include a layout in search.php or it'll break the one on index.php

if (isset($_POST['submit'])) {
    if (isset(
$_POST['query']) && $_POST['query'] != '') { // OR if(!empty($POST['query'])) { 
      
echo "You searched for: " . $_POST['query'];
    } else {
      echo 
"You didn't enter a search term. Wanna try again?";
    }
}
?>

Code
<html>
[b]<!--[/b] index.php -->

<form name="search" method=POST>
<input type="text" name="query"> <input type="submit" name="submit">
</form>
<br />
<?php include('search.php'); ?>
</html>
Great response guys :)

I did get this working just by posting my php inside the index file.

I will the include method next time!

Thanks


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Javascript]  by de_g3ntl3m4nDec 15, 2020 03:43 AM 0 106 Dec 15, 2020 03:43 AM
Last Post: de_g3ntl3m4n
[PHP] by AussieMay 3, 2020 06:00 AM 2 142 May 3, 2020 06:00 AM
Last Post: Aussie
[Javascript]  by Ex NihiloMay 15, 2019 08:36 AM 5 160 May 15, 2019 08:36 AM
Last Post: Ex Nihilo