1

I have a rather complex question. I'm currently building a page that searches tags in a mysql table, and displays results (without reloading or redirecting). The tags are user-input, and handled with javascript.

Here's my current layout -

Javscript AJAX Call

$('#search').on('input', function() {   //this is my search input
    item = $('#search').val();  //gets keyword from input
    $.ajax({
        type: "POST",
        url: "mysql/login.php", //not sure if this should be blank
        data: {'tags': item},  //passes keyword to PHP
        success: function(data){
            alert('success');  //this returns correctly
        }
    });
});

My Ajax seems to work correctly as it displays the "success" message when I type.

Here's my PHP, in a separate file. I'm certain I got all of the database connections correct, so I'll just include excerpts of interest.

<?php
if (isset($_POST["tags"]) && !empty($_POST["tags"])) {
    $submit = "%" . $_POST['tags'] . "%";  #takes the AJAX data
}else{  
    $submit = '';  #starts out blank
}

if ($result = $mysqli->query("SELECT * FROM instruments WHERE tags LIKE '" . $submit . "'")) {  #this selects only the tag submitted.  When the variable is defined without AJAX, this works correctly.

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        echo $row["name"];   #as a test, I'm displaying the name of the row.  
    }

    /* free result set */
    $result->free();
}    
?>

So far, both of these items work correctly, but independently. My problem is figuring out how to update my PHP MySQL query each time the text in the input changes, and changing the file on the same page to display the correct database entries.

In my HTML, I simply have

<?php include 'mysql/login.php' ?>

I have absolutely no idea where to start, and other questions on this site haven't proved helpful. Thanks

5
  • Not entirely sure I understand you. I think you're asking how to pass data from js to php. Is that it? Commented Jun 4, 2014 at 0:25
  • Is there a reason for that? I wanted to have the results automatically update as the user types. Could that lead to slow performance? (I only have several entries in my table) Commented Jun 4, 2014 at 0:29
  • Why should I use that instead of on('input')? I was under the impression that they did mostly the same thing. Commented Jun 4, 2014 at 0:32
  • You update your data with $mysqli_connection->query("INSERT instruments (column_name1,column_name2) VALUES ('string1','string2') WHERE column_name='some_string_value'");. Question was unclear. Commented Jun 4, 2014 at 0:41
  • My apologies - I was updating my query, not inserting any values. Commented Jun 4, 2014 at 1:59

1 Answer 1

1

You are not using the data you get back from your php script:

    success: function(data){
        alert('success');  //this returns correctly
    }

should be something like:

    success: function(data){
        $('#where_you_have_your_content').html(data);
    }

Assuming of course that your php is located in your mysql/login.php script, which seems a strange name for a search script.

Sign up to request clarification or add additional context in comments.

1 Comment

That was embarrassingly easy! Thanks. And yeah, it's pretty odd. I'd not much of a MySQL guy, so I'm still not very read up on naming conventions. Thankfully this is just personal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.