Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End [Solved] Form Not Returning Results when Coupled with JQuery

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #27717
    EamonnMac
    Member

    Hey guys, could use some help with this – it’s my first non-wordpress effort with PHP and my first ever effort with mySQL and my first functional piece of JQuery! So!

    Anyway I have a form here http://www.oldkitbag.com/form/resource.php that worked fine when the results showed up on a new page (I guess through using the _POST function). But I tried to add a little something to the form where the results would be appended by JQuery to the forms’ container div – i.e. on the same page.

    Now, due to the error msg, I know that the form is still talking to the PHP processor, but it’s not returning any results anymore. Also I now have the added problem that any new queries are appended onto the old ones at the bottom of the container rather than replacing them. Any ideas on how to solve these problems?

    Here’s the code for the form:

    Code:

    Form 1





    • To View all Resources available, leave the Subject Selection at ‘blank’ and leave the input bar Empty.
    • To View all Resources available within a particular subject, simply select that subject and leave the input bar empty.
    • To View all Resources available of a particular type, leave the Subject Selection at ‘blank’ and Enter ‘Book’, ‘Journal’, ‘Video’, or ‘Manual’ in the input bar.

    And here’s the Processor:

    Code:
    ” . “

    Resource Subject Title Author Amount in Stock

    “;

    $host = “xxxx”;
    $user = “xxxx”;
    $password = “xxxx”;
    $reqdatabase = “xxxx”;

    $mysql = new mysqli($host,$user,$password,$reqdatabase) or die(‘you’re dead’);

    $filtercategory = $_POST[‘category’];
    $filtersubject = $_POST[‘resourcesubject’];
    $filterterm = $_POST[‘filter’];

    if( $filtersubject == “” ) {
    $result = $mysql->query(“SELECT * FROM resourcelist
    WHERE $filtercategory LIKE ‘%$filterterm%'”)
    or die(‘Oops! Something went wrong. Why don’t you try again?’);
    }else {
    $result = $mysql->query(“SELECT * FROM resourcelist
    WHERE $filtercategory LIKE ‘%$filterterm%’ AND Subject = ‘$filtersubject’ “)
    or die(‘Oops! Something went wrong. Why don’t you try again?’);
    }

    if($result) {

    while($row = $result->fetch_object()) {
    $title = $row->Title;
    $author = $row->Author;
    $amount = $row->resourceAmount;
    $subject = $row->Subject;
    $description = $row->Type;

    $author = htmlentities($author);
    $title = htmlentities($title);

    echo “

    ” . “

    $description

    ” . “

    $subject

    ” . “

    $title

    ” . “

    $author

    ” . “

    $amount

    ” . “

    “;

    }
    }

    echo “

    “;
    ?>

    So, in short, why is my Processor no longer returning results, and how can I hide those results when I hit the submit button again (although I know that’s really a JQuery question!)?

    Bearing in mind this form is my first foray into the field and is literally patched together from various tuts, any help in cleaning it up would also be greatly appreciated – if that’s not pushing my luck too far! :D

    Ta in advance.

    #69901
    EamonnMac
    Member

    Aha! I was trying to do something like that on another trial page just now! Couldn’t get it to work at all though. I will certainly try your script. My problem with it originally though was the ‘data’ line – I didn’t understand how this worked and communicated with the processor script. If you could go into it in a little more detail I’d be most grateful. Particularly how it translates to my existing ‘receiving’ script of

    Code:
    $filtercategory = $_POST[‘category’];
    $filtersubject = $_POST[‘resourcesubject’];
    $filterterm = $_POST[‘filter’];

    The error script is "Warning: mysqli::query() expects at least 1 parameter, 0 given", so seemingly, my _POST functions aren’t all that they should be… :?

    Thanks again!

    #69916
    EamonnMac
    Member

    Excellent. Will try that now and let you know how I get on.

    #69917
    EamonnMac
    Member

    No joy, I’m clearly doing it wrong again. Here’s what I’ve written:

    Code:
    $(function() {
    $(‘.button’).click(function() {
    var data = “category=”+$(“input#category”).val()+”&”;
    data += “resourcesubject=”$(“input#resourcesubject”).val()+”&”;
    data += “filter=”$(“input#filter”).val();

    $.ajax({
    type: “POST”, // We are goin to submit the form using the POST method
    url: “results.php”, // The AJAX call will look for the response from this URL
    data: category=category&resourcesubject=resourcesubject&filter=filter, //pass in the POST data we defined in the data variable
    success: function(results) { // If the AJAX request is successful
    $(‘#tablediv’).html(results); // Put the returned data in the searchResults div
    }
    });
    return false;
    })
    });

    Being honest, and sorry for being so thick, I still don’t get the whole data thing. At all. Everything else I can read and understand, but here we seem to be establishing our data variable twice?

    #69995
    EamonnMac
    Member

    I get easily confused! :D

    Anyway, changed that, but nothing doing – not even an mySQL error msg.

    Just so we’re still on the same page, here’s the code for the two pages so far:

    Form page:

    Code:




    Form 1





    • To View all Resources available, leave the Subject Selection at ‘blank’ and leave the input bar Empty.
    • To View all Resources available within a particular subject, simply select that subject and leave the input bar empty.
    • To View all Resources available of a particular type, leave the Subject Selection at ‘blank’ and Enter ‘Book’, ‘Journal’, ‘Video’, or ‘Manual’ in the input bar.


    and processor (‘results.php’)

    Code:
    ” . “

    Resource Subject Title Author Amount in Stock

    “;

    if ( $filtercategory == “” ) {
    echo ‘mysql_error’;

    }elseif( $filtersubject == “” ) {
    $result = $mysql->query(“SELECT * FROM resourcelist
    WHERE $filtercategory LIKE ‘%$filterterm%'”)
    or die(‘Oops! Something went wrong. Why don’t you try again?’);
    }else {
    $result = $mysql->query(“SELECT * FROM resourcelist
    WHERE $filtercategory LIKE ‘%$filterterm%’ AND Subject = ‘$filtersubject’ “)
    or die(‘Oops! Something went wrong. Why don’t you try again?’);
    echo $query;
    }

    if($result) {

    while($row = $result->fetch_object()) {
    $title = $row->Title;
    $author = $row->Author;
    $amount = $row->resourceAmount;
    $subject = $row->Subject;
    $description = $row->Type;

    $author = htmlentities($author);
    $title = htmlentities($title);

    echo “

    ” . “

    $description

    ” . “

    $subject

    ” . “

    $title

    ” . “

    $author

    ” . “

    $amount

    ” . “

    “;

    }
    }
    echo “

    “;
    ?>

    Thanks for all your help – seriously, not a clue otherwise!

    #70092
    EamonnMac
    Member

    Any luck? Am I destined to wander forever in the land of two-page forms? Is there no hope doctor? :(

    #70127
    EamonnMac
    Member

    Aha! Maybe there’s hope for me yet! :D

    It’s this one:

    Code:
    data += “resourcesubject=”$(“input#resourcesubject”).val()+”&”;

    the second line of the fuction:

    Code:
    $(function() {
    $(‘.button’).click(function() {
    var data = “category=”+$(“input#category”).val()+”&”;
    data += “resourcesubject=”$(“input#resourcesubject”).val()+”&”;
    data += “filter=”$(“input#filter”).val();
    #70139
    EamonnMac
    Member

    Excellent! So now it reads

    Code:
    var data = “category=”+$(“input#category”).val()+”&”;
    data += “resourcesubject=”+$(“input#resourcesubject”).val();
    data += “filter=”+$(“input#filter”).val();

    I added a + to the third line as well, cos without it nothing happened. Now it’s reading an error:

    Quote:
    array(2) { ["category"]=> string(9) "undefined" ["resourcesubject"]=> string(25) "undefinedfilter=undefined" }

    but it is sort of working. The table headings were echo-ed even if it went pear shaped after that! Whoo Hoo! Thanks for sticking with it so far, by the way. Appreciate it.

    I’ve tried messing around with the input ID’s and Names, as that seems to be where it’s falling down, but even when it’s category=>category, subject=>subject, and filter=>filter, there’s no joy – still ‘undefined’. (Will this code work even if the value is left empty? ) When all inputs have values, the error persists.

    #70159
    EamonnMac
    Member

    IT WORKS!!!!!!! :D :D :D

    Really appreciate you sticking with it. Thanks bud, you are at One with the Quan. :ugeek:

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘Back End’ is closed to new topics and replies.