2

I want to send id to php script.I think my code is ok, but when i click on tag i get this error:

Notice: Undefined index: id in C:\xampp\htdocs\domaci\cao.php on line 4

here is my html + ajax code :

<html>
    <head>
        <script type="text/javascript" src="jquery-1.11.1.js"></script>
        <script type="text/javascript" language="javascript">       
        $(document).ready(function(){
            $('a').click(function(){
                $.post($(this).attr('href'), { id : $(this).attr('id') } );
            });
        });
        </script>
    </head>
    <body>
        <a href="cao.php" id="Barselona" >Barselona</a>
        </br>
        <a href="cao.php" id="Beograd" >Beograd</a>
    </body>
</html>

and this is cao.php:

<html>
    <body>
        <?php 
            $id = $_POST['id'];
            echo $id;
        ?>
    </body>
</html>

i really need this to get to work, please help me :)

1
  • The click will cause the browser to follow the link, you have to handle it and stop it Commented Jun 2, 2014 at 9:16

5 Answers 5

1

Your $.post ajax is fine. The issue is when the anchor is clicked, you don't preventDefault or return false, to stop the browser redirecting to cao.php. When it redirects to cao.php, it is a GET request, and so triggers the undefined index notice because there is no post data.

Add return false or preventDefault:

$('a').click(function(e){
    e.preventDefault();
    $.post($(this).attr('href'), { id : $(this).attr('id') } );
});

Look at your browser's console (F12) on the Network tab, to see the response of the ajax.

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

2 Comments

when i add e.preventdefault and i click on <a> tag i stay on my html page
i looked in console and i think ajax is ok, is there ant other way to redirect on next page beside <a>
0

Try it now.

$(document).ready(function(){
    $('a').click(function(){
        $.post($(this).attr('data-url'), { id : $(this).attr('id') } );
    });
});
</script>
</head>
<body>
<a href="#" data-url="cao.php" id="Barselona" >Barselona</a>
</br>
<a href="#" data-url="cao.php" id="Beograd" >Beograd</a>
</body>
</html>

1 Comment

url is not a valid attribute on a link. You'll have to use a data attribute: 'data-url'. It's also recognized by jQuery, so that you can just use $(this).data('url')
0

Your link redirects you, so simpe do this:

e.preventDefault();

Also its better to have variables (for future), here is EXAMPLE

Comments

0

Try this one will work for you:

<html>
        <head>
            <script type="text/javascript" src="jquery-1.11.1.js"></script>
            <script type="text/javascript" language="javascript">       
            $(document).ready(function(){
                $('a').click(function(){
                    $.post('cao.php', { id : $(this).attr('id') },function(data){
                        alert(data);
                     });
                });
            });
            </script>
        </head>
        <body>
            <a href="#" id="Barselona" >Barselona</a>
            </br>
            <a href="#" id="Beograd" >Beograd</a>
        </body>
    </html>

AT cao.php page

<?php 
            $id = $_POST['id'];
            echo $id;die;
        ?>

Comments

0

just try print_r($_POST) in cao.php & check the values are posting

$id=$_POST["id"];

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.