1

Hi I want to send data to the server and then have the php file save it. But currently I can't get even a simple POST to work.

This is the javascript and html part: This function is declared in the head:

function sendTableToServer(){
    var xhttp;
    if (window.XMLHttpRequest){
        xhttp=new XMLHttpRequest();
    } else {
        xhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xhttp.open("POST","http://music.collwyncraig.info/hajimama/save_setlist.php",true);
    xhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xhttp.send("fsong=AbandonSeoul");
}

Then this html is in the body:

<button type="button" onclick="sendTableToServer()">Save Setlist</button>

This is the php file

<?php
if (isset($_POST["fsong"])){
    echo 'trying';
    $song = $_POST["fsong"]; 
    echo $song;
    echo 'ok';
}
?>

When I click the button, nothing happens. If I use a <form> and a submit button, the php file is loaded by the browser, and I can access the input. However, I want to use a function like this because the information I want to send is going to be taken out of the html page using javascript and HTML DOM. So, why doesn't anything happen? :)

2
  • 4
    Where is your callback for XHTTP? Commented Apr 7, 2012 at 4:59
  • 1
    Ever thought of using jQuery? Commented Apr 7, 2012 at 5:01

4 Answers 4

1

If you want the same behavior as a form except you want to get data from the page, just create a form and submit it:

var form = document.createElement('form');
form.action = 'http://www.example.com/do-something';
form.method = 'post';
var song = document.createElement('input');
song.type = 'hidden';
song.name = 'song';
song.value = 'Song Name';
form.appendChild(song);
form.submit();
Sign up to request clarification or add additional context in comments.

8 Comments

This causes a postback. The whole reason to use AJAX is to avoid this. Why wouldya propose this?
@Visionary: My interpretation of the question was they wanted to replace the whole page as if it was submitted via a form, but they wanted to use AJAX because they needed data dynamically calculated.
Just use return false in the onSubmit event, or event.preventDefault to stop the page from reloading. That one missing detail shouldn't detract from the main idea of this solution. +1
@jmort253: Both of those solutions will prevent the data from getting sent to the server, making it pointless. However, setting the form's target to an iframe would do it. (although if that's what you want, might as well use AJAX (correctly))
@icktoofay - I'm using this technique successfully with the form onclick event. From what I understand it should work with onsubmit as well. Feel free to rollback my edit if you know that to be incorrect.
|
1

If you do not want to use jQuery ajax, try using pure ajax:

<html>
    <head>
    <script type="text/javascript">
    sendTableToServer()
    {

    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
        else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        //document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","http://music.collwyncraig.info/hajimama/save_setlist.php?fsong=AbandonSeoul", true);
    xmlhttp.send();
    }
    </script>


<?php
if (isset($_GET["fsong"])){
    echo 'trying';
    $song = $_GET["fsong"]; 
    echo $song;
    echo 'ok';
}
?>

Source: http://www.w3schools.com/php/php_ajax_php.asp

3 Comments

THERE'S NO NEED TO SHOUT!</capslock>
@Gary Look at his nickname :)
this seems close to what I had except using GET instead of POST. and am I correct that you missed writing in the name of the .php file in the xmlhttp.open() method?
0

If you're making a POST to a domain that isn't the same as what is in your address bar, it will fail. You cannot make cross-domain AJAX requests:

xhttp.open("POST","http://music.collwyncraig.info/hajimama/save_setlist.php",true);

Try removing the scheme and domain:

xhttp.open("POST","/hajimama/save_setlist.php",true);

This will make the POST request relative to the current domain as the http and domain are not needed here.

1 Comment

ah yah. actually it's the same domain, i just had written the full URL to check that using only the file name wasn't the problem. thanks.
0

Remember that AJAX is by nature asynchronous. You should be defining callbacks for completion and error conditions, as your data will not return right away. You may want to follow this Google tutorial on AJAX. JQuery does simplify the process, if available.

If your only goal is to send data to the server to write to a file, you don't need AJAX or Javascript. Just set the <form action="http://music.collwyncraig.info/hajimama/save_setlist.php"> and put the data you want to send to the server in the form tags. The only reason to use Javascript for AJAX is for "nicer behavior", which may be more complex than the simple usage you are looking for.

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.