2

I need to pass some js variables from index.php to php variable on tags.php
- without using forms
- without using address bar
- and go to tags.php

index.php

$('#m1tags').click(function(){
var id = $('.pmarked').data('id'); // an integer
$.ajax({
    url: 'tags.php',
    type: 'post',
    data: {'id': id},
    success: function() {

    }
});
location.href='tags.php';
});

tags.php

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

Notice: Undefined idex: id...

Any help?

3
  • 2
    you need to send a form instead of an ajax call! Right now you send everything to tags.php, but then "refresh" tags.php without the passed vars. Commented Apr 16, 2017 at 8:55
  • 1
    this will help: stackoverflow.com/questions/133925/… Commented Apr 16, 2017 at 8:56
  • 2
    Possible duplicate of JavaScript post request like a form submit Commented Apr 16, 2017 at 8:58

2 Answers 2

2

The request you made is asynchronous which means that it's happening in the background and then the script continues to execute on the same page. You are sending a POST request and then redirecting user using a different request.

You can send a hidden form using JavaScript so the user will be redirected there along with POST data in the same request.

window.onload = function () {
    var id = 60;
    $("#id-input").val(id);
    $("#tags-form").submit();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<form id="tags-form" method="POST" action="tags.php">
<input type="hidden" name="id" id="id-input">
</form>

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

Comments

0

Here you are trying to make "tag.php" get infos from an unknown source. To send info to a php page you need to use a form or a url variables.

Even when you use ajax url variables and post variables are used (when a user's related infos are needed).

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.