0

Hi all!

I have a problem which I can't seem to fix. I would like to know is it possible to send a normal JS variable to a php page. I've tried the following and nothing seems to work.

<input type="submit" value="Bookmark" onclick="bookmark();">

function bookmark() {
  var pageNum = 2;
  var xmlhttp;
  if (window.XMLHttpRequest)
  {
     xmlhttp = new XMLHttpRequest();
  }else{
     alert("Not assigned!");
  }

   xmlhttp.open("GET", "Bookmark.php?pageNum="+pageNum, true);
   xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); with or without this it doesn't work
   xmlhttp.send();
   alert("Bookmark send!");
}

Then in the php:

$pageNum = $_GET['pageNum']; 
echo "Page numbers:" + $pageNum;

I've tried post aswell and I've tried

var pageNum = 'pageNum=' + 2;
xmlhttp.open("POST", "Bookmark.php", true);
xmlhttp.send(pageNum);

php:

$pageNum = $_POST['pageNum'];
echo "Page numbers:" + $pageNum;

any help will be appreciated!

Thanks!

Edited by Phillamon

4
Contributors
7
Replies
30
Views
4 Years
Discussion Span
Last Post by Phillamon
0

I think xmlhttp.send(null);should be used instead ofxmlhttp.send();,because no arguments doesn't mean null in php

0

also the concatenation operator should be ampersand not "+" here?pageNum="+pageNum,

0
  1. You do notting with the return value

        function bookmark() {
        var pageNum = 2;
        var xmlhttp;
        if (window.XMLHttpRequest)
        {
        xmlhttp = new XMLHttpRequest();
        }else{
        alert("Not assigned!");
        }
        xmlhttp.onreadystatechange=function()
            {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                document.getElementById("myDiv").innerHTML="we got back: "+xmlhttp.responseText;
                }
            };
        var url="Bookmark.php?pageNum="+pageNum;    
        xmlhttp.open("GET",url , true);
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
        xmlhttp.send();
        alert("Bookmark send!");
        }
    
    <form>
        <input type="button" value="Bookmark" onclick="bookmark();">
     </form>
    <div id="myDiv"></div>
    
  2. your php should be

    $pageNum = $_GET['pageNum'];
    echo "Page numbers: $pageNum";
    

3 change input type from submit to button

Edited by pzuurveen

0

Thank you for the response.

@rouf - I can't change it to ampersand as I'm using javascript and not jquery. I've tried the send(null) which did unfortunately not work.

@pzuurveen - Thank you for pointing that out in your first point. I wanted to handle the data on the php side that I didn't think yet what to do with it when it returned.

It's weird now because on the php side I get that undefined index error of the variable pageNum. But with the response text it prints out the value correctly...Which is confusing. So it means that the variable data was never reached by the php?

0

So after some research I saw that I could do this via a cookie request. Very simple and hopefully it won't give any errors in the future.

document.cookie = 'pageNum='+pageNum;

then on the php:

$pageNum = $_COOKIE['pageNum'];
echo "Page numbers: $pageNum";

If someone could maybe still respond to the previous comment! That would be great! As I'm curious to know why it didn't work.

Will mark it as solved after I got an answer :)

Edited by Phillamon

0

I can't see any reason why you can't send data (whether originally in a js var or in a static string) to a php page via ajax.

I tried your code:

<html>
<head></head>
<body>
<input type="submit" value="Bookmark" onclick="bookmark();">
<script>
function bookmark() {
  var pageNum = 2;
  var xmlhttp;
  if (window.XMLHttpRequest)
  {
     xmlhttp = new XMLHttpRequest();
  }else{
     alert("Not assigned!");
  }
   xmlhttp.open("GET", "Bookmark.php?pageNum="+pageNum, true);
   xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   xmlhttp.send();
   alert("Bookmark send!");
}
</script>
</body>
</html>

With the following in the php file:

<?php
file_put_contents("me.txt", $_GET['pageNum']);
?>

And it created a me.txt file with the contents, 2

So fine AFAIK.

//EDIT

Did you expect the php code to echo something to the screen? If so, no that won't happen as any output from php needs to be returned to js so that the js can output it.

Edited by diafol

0

Thanks for the reply!

Yes I did expect the php to echo the value to the screen. Because later I want to save that variable to a database so I just wanted to test if the server correctly received the value.

Thanks for the help!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.