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!

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

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

  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

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?

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 :)

Member Avatar for diafol

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.

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.