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?

qadenza
  • 7,864
  • 17
  • 50
  • 92
  • 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. – Jeff Apr 16 '17 at 08:55
  • 1
    this will help: http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit – Jeff Apr 16 '17 at 08:56
  • 2
    Possible duplicate of [JavaScript post request like a form submit](http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit) – Jeff Apr 16 '17 at 08:58

2 Answers2

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>
Marcin
  • 1,514
  • 1
  • 12
  • 27
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).

M.Rasheed
  • 26
  • 4