0

I am trying to get the value of a textbox without pass through form actions (because i cannot neither redirect to another page or refresh the current one).

index.php

into head

<script type="text/javascript">
    function send() {
       $.ajax({
       type:"POST",
       url: "script.php",
       data: {
              fname: document.getElementById("fname").value,
              lname: document.getElementById("lname").value
             },
       success: function callScriptAndReturnAlert() {
                     var sdata = new XMLHttpRequest();
                     sdata.onload = function() {
                        alert(this.responseText);
                     };
                     sdata.open("get", "script.php", true);
                     sdata.send();
                  } 
              });
        }
    </script>

into body

<button class="myclass" onclick="send();">MyButton</button>
<input type="text" id="fname" placeholder="First name here" />
<input type="text" id="lname" placeholder="Last name here" />

Button is before input just for UI stuffs

script.php

    $prev = $_SESSION['prev'];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];

    [....do something....]

    echo 'You used '.$fname.' and '.$lname;

Well.. the script.php doesnt receive the value of the inputs fname and lname.

1
  • I do get the values. Is this all the code? Didn't you include jQuery? It works if you include it. Why do you do a second ajax call when the first call to script.php completes? And why don't you use jQuery? Commented Mar 4, 2015 at 16:11

2 Answers 2

2

You are trying to make two ajax calls when you only need one.

<script type="text/javascript">
    function send() {
      $.ajax({
       type:"POST",
       url: "script.php",
       data: {
              fname: $("#fname").val(),
              lname: $("#lname").val()
             },
       beforeSend: function(){
           alert('Sending');
       },
       success: function(data) {
              //Received data from PHP script
              alert(data);
       },
       error: function(){
           alert('Error !');
       },
       complete: function(){
           alert('Done');
       }  
     });
   }
    </script>
Sign up to request clarification or add additional context in comments.

Comments

0
$.post(
    'script.php', // Your PHP file
    { // The data to pass
        'fname' : $('#fname').val(),
        'lname' : $('#lname').val()
    }
).done(function(data) { // Here your AJAX already finished correctly.
    alert(data); // Show what the PHP script returned
});

The $.ajax(), $.get() and $.post() jQuery methods are for making AJAX calls and, due to that, they already manage their own XMLHttpRequest object.

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.