0

I've been looking at other questions related to my problem however none seem to correspond to my exact circumstances. I'm attempting to create a javascript variable which I will POST with Ajax to a PHP variable further down in my code. After hopefully setting the variable correctly I attempt to POST and var_dump the result however it is not the "0" I had hoped for but rather a frustrating bool(false). I've read that this may be a problem with the url (as I POST from the same file) or perhaps some other issue to do with the transfer of data to the Ajax request though I have as yet been unable to work out the issue. Testing for the data in the console also shows nothing so I've been forced to ask here. Thank you for any help or guidance.

Javascript

jQuery(document).ready(function(){var offset = parseInt("0");
        $.ajax({url: 'index.php', type: 'POST', data: offset, success:function(data){return data;}});
    });

PHP

<?php var_dump(isset($_POST['offset']));
4
  • did you tried with dataType: 'text' ? Commented Dec 10, 2015 at 8:28
  • yep! everything returns the same bool(false) response I'm afraid Commented Dec 10, 2015 at 8:29
  • Convert your offset to string '0' then change it on server side. Commented Dec 10, 2015 at 8:30
  • @aldrin27 How exactly do you mean? Commented Dec 10, 2015 at 8:35

6 Answers 6

3

add an index called offfest

$.ajax({
    url: 'index.php', 
    type: 'POST', 
    data: {offset:offset}, 
    success:function(data){
        return data;
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

This was how I had set up the request earlier (changed it just to see if trying anything different would work). Giving a shot now hasn't changed anything though so I think there is something else going on.
hmm. are there any console errors? and can you see the request being sent to the server?
3

you can try with :

jQuery(document).ready(function(){
    $.ajax({url: 'index.php', type: 'POST', data: {offset: parseInt("0")}, success:function(data){return data;}});
});

1 Comment

I feel like your answer should definitely work but it just returns the same answer. There's got to be something else going on in the background that I'm not aware of. Thank you though, this was very useful
2

I'm not totally familiar with PHP but in your javascript code, data should be dictionary for Ajax post. something like {'offset': 0}

Comments

1

Your problem is that you're using var_dump in your php.
That literally returns the string: "bool(true)".

Use var_export, instead.

Instead of:

<?php var_dump(isset($_POST['offset'])); ?>

Use:

<?php var_export(isset($_POST['offset'])); ?>

var_export will output the string representation of the value, instead of the type / value combo var_dump returns.


If you just want to return the posted value, do this:

<?php var_export($_POST['offset']); ?>

Or:

<?php echo json_encode($_POST['offset']); ?>

Just keep in mind that you need to pass the data to the post, correctly:

data: { offset: offset }

Instead of:

data: offset

12 Comments

Let me tell you I am having a hell of a time testing all of these options. This now returns false
As OP said: "however it is not the "0" I had hoped for but rather a frustrating bool(false)". @Cerbrus, try my answer.
ReneKorss: That would mean the OP literally wants returned what he's sending to the php. @masteryupa: What exactly do you want returned from the server?
Right. Now all you need to do is properly pass the data to the php call, as mentioned in other answers: data: { offset: offset }
Hm, that's weird. No idea why that error is showing.
|
1

Js:

var offset = parseInt("0");
$.ajax({url: 'index.php', type: 'POST', data:{offset: offset},success:function(data){alert(data.offset);}});

php:

<?php echo json_encode(array('offset'=>isset($_POST['offset'])));

2 Comments

Thanks for your reply. Am now receiving this error Notice: Use of undefined constant offset - assumed 'offset' in /Applications/XAMPP/xamppfiles/htdocs/parable/index.php on line 149 {"offset":false}
Thank you. Now simply {"offset":false}
0

you should give a name to the offset variable so , change your data parameter into a JSON format :

$.ajax({url: 'index.php', type: 'POST', data:{ 'offset': offset},success:function(data){return data;}});

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.