1

Here's my scenario:

I have a drop down menu, with different values. When option 'A' is chosen, I want it to use JQuery to call a Php script. That Php script will make a database query which returns a record ID. When that record ID is returned to the javascript, javascript will direct the browser to a particular URL based on the returned ID (i.e. /products/$ID)

Right now, I have it so that the dropdown menu triggers a javascript function. I also have the Php script that does the database work. Im just not sure what should go in the javascript function, or how to make it connect with he Php script, and how I get the returned data (and how to deal it with the return type -- xml, html, etc)

Im using JQuery library, and Php 5.x.

1
  • yeah, i know I those are the steps, but im unsure of how the implementation works. Sorry, I always forget in the heat of things to click 'accept' when im finding my answer ! :) Commented Apr 19, 2011 at 20:05

2 Answers 2

2

A simple example would be to take a form like

Example.php

<form id="form" name="form">        
    <select name="dropdown" id="dropdown">
        <option value="A">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
        <option value="D">D</option>
    </select>

    <input type="submit" id="submit" name="submit" value="Submit!" />
</form>

Then link it with some JQuery to intercept the form, and send it to a PHP file

JQuery.js

$(document).ready(function(){
    $("#dropdown").change(function(event) {
        $.ajax({
            type: "POST",
            url: "query.php",
            data: $('#form').serialize(),
                datatype: "json",
            success: function(data){
                var ret = jQuery.parseJSON(data);
                    // Redirect to ret.link
            }
    });
    event.preventDefault();
    });
});

Then, create the PHP file to interpret it

query.php

$stmt = $sql->dbh->prepare("SELECT `Link` FROM `Links` WHERE `ID` = :id");

$stmt->bindValue(':id', $_POST['dropdown']);

$stmt->execute();

$link = $stmt->fetch()['Link'];

echo json_encode(array('link' => "{$link}"));

die();
Sign up to request clarification or add additional context in comments.

Comments

1
$("#menu-item").click(function() {

    jQuery.ajax({
        type: "POST",
        url: "yourScript.php",
        data: "data=foo",
        success: function(response){
            //redirect to id using response
            window.location.replace("http://yoursite.com/products/" + response);
        }
    });

});

Of course, this will need to be customized to your unique situation.

3 Comments

but what format is 'response' in? and how would I access the data within it?
response is any data that is returned from your php script. this can be anything from JSON to HTML to a string. JSON is usually the best way to send the data back and forth, though, as jQuery has an excellent .parseJSON function
thanks for that, im almost there. still having some issues with the return but ill figure it out

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.