0

I am trying to run a php script that pulls data from a DB with the click of a button. I want to use ajax so the page doesn't refresh. I've tested using normal post / submit with page refresh and it works but I am having trouble using ajax to display the data. This is my code:

  <form method="get" id="ghosts">
         <div id=rightc>
              <h7>Select Hosts:</h7>
     <br></br>

      <select id="group" name="group" onchange='this.form.submit()'>
           <?php
        $link=mysql_connect($mysqlserver, $username, $password) or die ("Error connecting to mysql server: ".mysql_error());
        mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());
        $gquery=" SELECT groupname FROM groups";
        $gresult=mysql_query($gquery) or die ("Query to get data from firsttable failed: ".mysql_error());
        while ($grow=mysql_fetch_array($gresult)) {
        $groupname=$grow["groupname"];
            echo "<option>$groupname</option>";
        }
        ?>
    </select>
     <input type="hidden" name="ghosts" value="1" />
     <noscript> <input type="submit" name="ghosts" id="group" value="Choose Group" /></noscript>

  </form>
spyda46
  • 19
  • 5
  • 1
    Where are the input fields of the form? – Sougata Bose Aug 26 '15 at 12:19
  • 1
    Use GET method and getting value using POST `isset($_POST['ahosts'])` – Saty Aug 26 '15 at 12:22
  • 1
    Big clue. **`$(".result")` matches nothing.** – Popnoodles Aug 26 '15 at 12:22
  • You should edit your question with you form code and (if possible), your 'search.php' code. – Peter Aug 26 '15 at 12:24
  • @Peter the form code is in there and the PHP is not neccessary, though confirmation that it returns data would be good. – Popnoodles Aug 26 '15 at 12:24
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 26 '15 at 12:26
  • @Popnoodles you're right about the form code, but I think that the PHP code may help because we could see the way he is getting the form data – Peter Aug 26 '15 at 12:28
  • 1
    To use AJAX effectively you should put your PHP with its requests to MySQL in a separate file. You would then make the AJAX request to the PHP file [and return the data](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard Aug 26 '15 at 12:28
  • Hi thanks for all looking, the code is all self contained so this is search.php. The form is simple its just a button to trigger the script. It is show above in the code. Maybe there is a better way of doing this ? really what i want is a button that says search then runs the php code to pull data from the db using ajax – spyda46 Aug 26 '15 at 12:29
  • 1
    That's the same file that it's pulling in? So `` and `` and ` – Popnoodles Aug 26 '15 at 12:30

2 Answers2

4

If your search.php file returned HTML, and assuming that the ajax works (it looks like it should), you're trying to put the contents into an element that doesn't exist.

$(".result").html(data); 

It needs to exist on the page. Add this somewhere.

<div class="result"></div>

However, your PHP doesn't return any data because of this condition if(isset($_POST['ahosts'])) which requests a parameter that isn't in the form and uses the wrong request method.

You're using GET ($.get()), so in the PHP it needs to read

if(isset($_GET['ahosts']))

And add this inside your form or give the button a name

<input type="hidden" name="ahosts" value="1" />
<!-- or -->
<button type="submit" name="ahosts" />
Popnoodles
  • 28,090
  • 2
  • 45
  • 53
0

I think that you make a mistake here :

if(isset($_POST['ahosts']))

You check if isset() POST request but actually you send GET request ...

read the documentation https://api.jquery.com/jquery.get/

So if you change your code to:

 if(isset($_GET['ahosts']))

this will return true and now you will have a response

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • Guys many Thanks ! -- Popnoodles thanks very much your suggestions have rsolved the issue for me -- Great ! :) – spyda46 Aug 26 '15 at 12:56
  • Hi Guys, just a follow up question. I have a similar form to one above but this one uses a drop down menu to choose a "group" i've used the same logic with ajax as above, the page is working in the sense i get the results expected but the problem is the page is not refreshing, i added the new code above : – spyda46 Aug 26 '15 at 14:01
  • Hi, the main reason for using ajax is to prevent refreshing the page – E. Stancheva Aug 26 '15 at 14:11