3

I have many links like this... different link id me for different items

<a class="member" href="http//ex.com/action.php?me=1">link1</a>

so when I click this I want to navigate to action.php from here (here.php) I am using ajax to do this.

I'm new to jQuery.. I am not able to understand the usage the url parameter.. I tried many posts found here... wanted to the basic way to send the --me-- value from here.php to action.php..

$("a.member").click(function(e) {
    $.ajax({
        type: "GET",
        url: "action.php?",
        data: "me=" + me,
        success: function (data) {
            alert(data);
        }
    });

    return false;

    e.preventDefault();
});

3 Answers 3

10

Here is an example,

The jquery part:

$("#update").click(function(e) {
    e.preventDefault();
    var name = 'roger'; 
    var last_name = 'blake';
    var dataString = 'name='+name+'&last_name='+last_name;

    $.ajax({
        type:'POST',
        data:dataString,
        url:'insert.php',
        success:function(data) {
            alert(data);
        }
    });
});

The insert.php page

<?php
    $name = $_POST['name'];
    $last_name = $_POST['last_name'];
    $insert = "insert into TABLE_NAME values('$name','$last_name')";// Do Your Insert Query
    if(mysql_query($insert)) {
        echo "Success";
    } else {
        echo "Cannot Insert";
    }
?>

Note: do not use mysql_* functions

Hope this helps,

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

1 Comment

please don't promote the use of mysql_* functions. They are deprecated and security nightmares. in your example: mysql injection.
7

Try this :

$.ajax({
    type: "GET",
    url: "action.php",
    data: {
        me: me
    },
    success: function (data) {
        alert(data);

    }
});

5 Comments

ok so how do i access it from action.php,like $_GET['me'], will this do to get the values.. should i use somethin else.. And MANY THANKS for reply :)
also he should think about using json as transport data format. this would require the return format to be json encoded and is only necessary to validate data. doe not matter (that much) for raw html or similar. and it is something to think about after you solved the current problem.
@user2234992 yes, the values you send along are in $_GET (or $_POST depending on http command) and you can access them from there
i have another doubt.. i have this link with class member... this function doesn't seem to click.. i got this link as ajax response from action.php... does ajax behave differently to data got from another page.. sry if that is stupid question..am new here.. tryin2 figure out.. @scones
if your current problem is resolved, please chose your accepted answer (if one of them solved the problem). If the current problem is not solved, please give information us some information on what the problem seems to be by editing your original post. If your problem is resolved and you have a new problem, please try to fix it yourself for a while, then formulate a new question on stackoverflow.
2

Since the parameter is already in the <a>'s href url just pass that to url

$("a.member").click(function(e){ 
     $.ajax({
            type: "GET",
            url: this.href,
            success: function(data){
                      alert(data);
            }
     });
     return false;
}); 

Comments

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.