2014-05-15 86 views
0

我不確定它是否只是我或什麼,但這看起來很奇怪。當我點擊一個按鈕,我有jQuery發送JavaScript變量到一個PHP網站在那裏處理。然而,在PHP網站上,他們將其定義爲未定義的索引。奇怪的部分是,他們通過PHP的回聲顯示在HTML頁面上。注意:html按鈕是一個輸入類型=「按鈕」,而不是提交,因爲我不想重新加載頁面。jquery post變量顯示在html中,但不是php

的jQuery:

var timestampst = $(timestamp).val(); 

var objNamest = $(objInst).val(); 

$.post("sendCalc.php", { 
    postobjNamest:objInst, 
    posttimestampst:timestamp}, 
function(data){ 
    $("#divResult").html(data); 
}); 

PHP:

//used for troubleshooting, returns Array() on the php page and Array ( [posttimestampst] => 1399973296 [postobjNamest] => test2-1 
print_r($_POST); 

//when the if and else are used it the php page always echos Not Working, meaning that the $_POST is not set somehow. However, the html page shows the echoed variables in the "divResult" as it should. 

//when I try the code without the if and else, the php page returns Undefined Index: posttimstamp/postobjNamest. However, the html page still shows the echoed variables. 

if(isset($_POST["posttimestampst"])){ 
    $timestamp = $_POST["posttimestampst"]; 
    echo $timestamp; 
    echo "<br>"; 

    $objName = $_POST["postobjNamest"]; 
    echo $objName; 
    echo "<br>"; 
} 
else{ 
    echo "Not Working"; 
} 

任何幫助,不勝感激!

編輯:

//gets selected object from a dropdown menu 
selectedObj = document.getElementById("selectObj").value; 

//objName in javascript taken from $objName var in php that is and the beginning of the html page. 
objName = <?php echo json_encode($objName); ?>; 

//objInst takes the value of the dropdown menu and assigns it as the [] in objName array 
objInst = objName[selectedObj]; 

//timestamp is set in php and imported to java 
var timestamp = <?php echo $timestamp; ?>; 

編輯2:

<html> 
    <head> 
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type"> 
    <meta content="utf-8" http-equiv="encoding"> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js""> </script> 
    </head> 
<h3>Optionen und Berechnen</h3> 
    <form name="myForm" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post"> 
    <div id="divCalc"> 
    </div> 
    <input id="addObject" type="button" onclick="addObj()" value="Add Object"> 
    <br> 
    <br> 
    <div id="divAddObj" hidden="true"> 
    </div> 
    <br> 
    <div id="divCalc"> 
    </div> 
    <div id="divResult"></div> 
    </form> 
    <script type="text/javascript" name="addObject"> 

    var objName; 
    var selectedObj; 
    var objInst; 
    var timestamp = <?php echo $timestamp; ?>; 

     //Start select dropdown  
     var select_element = document.createElement("select"); 
     select_element.setAttribute("id", "selectObj"); 
     select_element.setAttribute("name", "selectObject"); 

     options = new Array(); 
     objName = <?php echo json_encode($objName); ?>; 
     for (var i = 0; i < (<?php echo $arrayNum; ?>); i++){ 
      options.push(new Option(objName[i], i, false, false)); 
     } 
      options[0].selected = true; 

     for (var option in options){ 
      select_element.appendChild(options[option]); 
     } 
     //End select dropdown 

     //check selected object 
     selectedObj = document.getElementById("selectObj").value; 

     objInst = objName[selectedObj]; 
     var timestampst = $(timestamp).val(); 
     var objNamest = $(objInst).val(); 

     $.post("sendCalc.php", { 
      postobjNamest:objInst, 
      posttimestampst:timestamp}, 
     function(data){ 
      $("#divResult").html(data); 
     }); 
</script> 
+0

分享的HTML代碼 – Jenz

+0

什麼是'objInst'的價值?你應該不使用'postobjNamest:objNamest'嗎? –

+1

什麼是'objInst'? – Sthe

回答

0

更改您的代碼:

objNamest = objInst.value; 
timestampst = timestamp.value; 
$.post("sendCalc.php", { 
    postobjNamest: objNamest, 
    posttimestampst: timestampst }, 
    function(data){ 
     $("#divResult").html(data); 
}); 
+0

仍然無法使用。 –

+0

您可以向我們展示**您的html輸入元素和按鈕**以及您定義** timestamp **和** objInst **變量的js代碼嗎? – PeterKA

+0

你可以發佈你的服務器發送給你的瀏覽器的HTML嗎?你還可以發佈JSON對象objName嗎? – PeterKA

0

你缺少的$.post()data參數。

從文檔約post()

數據

類型:PlainObject或字符串:

被髮送 與該請求的服務器A普通對象或字符串。

你PARAMS postobjNamest & posttimestampst存在爲$.post()方法

應該

$.post("sendCalc.php", { 
    // An "on-the-fly" created JavaScript object, which is valid 
    data: { 
     postobjNamest: objInst, 
     posttimestampst: timestamp 
    }, 
    function(data){ 
     var content = $.parseJSON(data); 
     window.console.log(content.postobjNamest); 
     window.console.log(content.posttimestampst); 
    } 
}); 

從文檔約parseJSON()

說明:取一好形成的JSON字符串,並返回得到的 JavaScript對象。


而在PHP:

$objName = $_POST['postobjNamest']; 
$timestamp = $_POST['posttimestampst']; 

// Returning your values to client 
// Don't echo them in PHP 
echo json_encode(array(
     'postobjNamest' => $objName, 
     'posttimestampst' => $timestamp 
    ); 

從文檔約json_encode()

json_encode - 返回一個值


的JSON表示

JavaScript對象:

// Declaration 
var Obj = { 
    propertyOne: 'value', // string 
    propertyTwo: 52.3654, // float 
    methodOne: function() { 
     // your function code 
    }, 
    methodTwo: function() { 
     // your function code 
    } 
} 

//Instances 
var objOne = new Obj(); 
objOne.methodOne(); 
objOne.propertyTwo; 

var objTwo = new Obj(); 
objTwo.methodOne(); 
objTwo.propertyTwo; 
+0

感謝您的回答,但是當我使用 $。員額( 「sendCalc.php」,{ \t數據:{ \t postobjNamest:objInst, \t posttimestampst:時間戳}, \t \t \t功能(數據){ \t \t \t \t $(「#divResult」)。html(data); \t \t \t}}); 我的添加對象按鈕(運行javascript併發送帖子)不起作用 –

+0

顯示html代碼請問,$ _POST是否包含了一些東西,現在呢? –

+0

查看編輯答案 –