-2

This is my simple form which contains employe name, empno, empsal and doj(date of joining). Here i'm posting the values of this form through XMLHttpRequest object through send asynchronously and updating the requestTxt in the DOM in myDiv.

Here when i pass the parameters some thing like this, i can able to get the values in php echo $empname. "is my name";

var params = "empname="+myname+"&empno="+myno+"&empsal="+mysal+"&empdoj="+mydoj;

But at the same time, if i pass it as a object, stringified i don't know why it is not printing

        var params = {
        "empname":myname,
        "empno":myno,
        "empsal":mysal,
        "empdoj":mydoj
    };      
    var jsonText = JSON.stringify(params);
    alert(jsonText);

Here is my code index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="windows-1252">
<title>Sample form</title>
<style>
table td{
    border-collapse:collapse;
    border:1ps solid #000;
}
.hide{
    display:none;
}
</style>
<script type="text/javascript" src="util.js"></script>
</head>
<body>
<form name="sampleForm" id="sampleForm" action="javascript:return false;">
    <table width="40%">
        <tr>
            <td>Employee Name:</td>
            <td><input type="text" name="empname" id="empname"/></td>
        </tr>
        <tr>
            <td>Employee No:</td>
            <td><input type="text" name="empno" id="empno"/></td>
        </tr>
        <tr>
            <td>Employee salary:</td>
            <td><input type="text" name="empsal" id="empsal"/></td>
        </tr>
        <tr>
            <td>Employee DOJ:</td>
            <td><input type="text" name="empdoj" id="empdoj"/></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" name="submitBtn" id="submitBtn1" /></td>
        </tr> 
    </table>
    <div class="hide" id="myDiv"></div>
            </form>
            <script type="text/javascript">
            var subform = document.getElementById("sampleForm");
         subform.onsubmit = function(){     
    var myReq;
    var uRLTxt = "loginCheck.php";
    var myname = document.getElementById("empname").value;
    var myno = document.getElementById("empno").value;
    var mysal = document.getElementById("empsal").value;
    var mydoj = document.getElementById("empdoj").value;
    /*var params = "empname="+myname+"&empno="+myno+"&empsal="+mysal+"&empdoj="+mydoj;      */
    var params = {
        "empname":myname,
        "empno":myno,
        "empsal":mysal,
        "empdoj":mydoj
    };      
    var jsonText = JSON.stringify(params);
    alert(jsonText);
    myReq = new XMLHttpRequest();
    myReq.open("POST",uRLTxt,true); 
    myReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
    //myReq.setRequestHeader("Content-type", "application/JSON; charset=utf-8");        
    myReq.setRequestHeader("Content-length", jsonText.length);
    //alert(jsonText.length);
    myReq.setRequestHeader("Connection", "close");
    myReq.onreadystatechange = function() {
       if(myReq.readyState == 4 && myReq.status == 200){
            var return_data = myReq.responseText;
            console.log(return_data);
            document.getElementById("myDiv").style.display = "block";
            document.getElementById("myDiv").innerHTML = return_data;
        }
    }
    myReq.send(jsonText);
    return false;
      };
        </script>
        </body>
        </html>

Php Code - loginCheck.php

<?php   
    $empname = isset($_REQUEST['empname']);
    $empno = isset($_REQUEST['empno']);
    $empsal = isset($_REQUEST['empsal']);
    $empdoj = isset($_REQUEST['empdoj']);
    echo $empname."is my name";
?>
4

3 回答 3

3

如果要单独使用javascript,则要在方法中指定要发送的数据send()

var params = {
        "empname":myname,
        "empno":myno,
        "empsal":mysal,
        "empdoj":mydoj
    };      
    var jsonText = JSON.stringify(params);
    myReq.send("totalJsonStr="+jsonText);

使用 . 解码您的 json 字符串json_decode()。将此添加到您的 loginCheck.php

<?php
        if(isset($_POST["totalJsonStr"])) {
        $jsonVal = json_decode($_POST["totalJsonStr"]);
        //If you want empname
        print $jsonVal->{'empname'} . " is my name"; }
        else { die("No Data Found"); } 
    ?>
于 2013-07-24T15:48:58.777 回答
0

Your PHP looks like this:

<?php   
    $empname = isset($_REQUEST['empname']);
    $empno = isset($_REQUEST['empno']);
    $empsal = isset($_REQUEST['empsal']);
    $empdoj = isset($_REQUEST['empdoj']);
    echo $empname."is my name";
?>

But the function isset() only returns true or false, depending if a variable is set or not. Your script should look like:

<?php
    // Repeat for all variables
    // This is an equivalent to:
    // if (isset($_REQUEST['empname'])) {
    //     $empname = $_REQUEST['empname'];
    // }
    // else {
    //     $empname = "";
    // }
    $empname = isset($_REQUEST['empname']) ? $_REQUEST['empname'] : "";

    echo "{$empname} is my name";
?>

For your params string in Javascript, change this:

var params = {
    "empname":myname,
    "empno":myno,
    "empsal":mysal,
    "empdoj":mydoj
};      
var jsonText = JSON.stringify(params);
alert(jsonText);

// Blah blah...

myReq.send(jsonText);

for this:

var params = {
    "empname":myname,
    "empno":myno,
    "empsal":mysal,
    "empdoj":mydoj
};
var out = new Array();

for (key in params) {
    out.push(key + "=" + encodeURI(params[key]));
}   
var postText = out.join("&");
alert(postText);

// Blah blah...

myReq.send(postText);
于 2013-07-24T11:26:12.950 回答
0

试试看,

<script language="javascript" src="jquery-1.4.4.min.js"></script> 
  <script>
 var myname = document.getElementById("empname").value;
 var myno = document.getElementById("empno").value;
 var mysal = document.getElementById("empsal").value;
 var mydoj = document.getElementById("empdoj").value;


 var subform = document.getElementById("sampleForm");
      subform.onsubmit = function(){   
    $.ajax({
  url: 'loginCheck.php', //This is the current doc
  type: "POST",
  dataType:'json', // add json datatype to get json
  data:"empname=myname&empno=myno&empsal=mysal&empdoj=mydoj",
  success: function(data){        alert(data);        })
   },    });      };
 </script>
于 2013-07-24T11:01:18.577 回答