2017-02-19 43 views
-2

我似乎无法理解如何将数据从我的客户端HTML发送到我的服务器端PHP(这已经意味着它们不在同一个文件夹中,并且不在服务器中运行)并且只能获取未知变量和致命错误的通知:无法访问空属性。如何使用JSON将HTML数据发送到PHP?

我尝试了W3Schools中的方法,但仍然没有运气。只是为了确保我试图复制粘贴它。还是一样。

所以我的问题是:我如何发送这个简单的客户端HTML/JavaScript的数据: -

<script> 
function sender(){ 
obj = "tblname"; 
// how to send that data to the php server-side. 
} 
</script> 

为了这个PHP: -

<?php 
header("Content-Type: application/json; charset=UTF-8"); 
$obj = json_decode($_GET["x"], false); 

$conn = new mysqli("localhost", "root", "", "mydb"); 
$result = $conn->query("SELECT * FROM ".$objData); 
$outp = array(); 
$outp = $result->fetch_all(MYSQLI_ASSOC); 

echo json_encode($outp); 
?> 

使用JSON?

如果任何人能够详细说明并给我看一个样本,那就太好了。

同样,我是使用JSON的noob/newb,没有长期的背景(我刚开始就像一个星期前一样,而且已经有很多问题了),并且当涉及到这种类型的客户端时,我完全无能为力到服务器通信。

我只需要简单的发送者代码(来自JavaScript)和接收代码(来自Php)一两行就可以了;并简要介绍他们如何工作。

我使用的是Windows 7,Wamp3.0.6和Chrome。 PS:我从W3Schools那里得到了这个。是的,它没有工作。请不要模糊。谢谢!

+0

等待回了一点。变量'obj =“tblname”;'从哪里来?它来自HTML表单吗?你介意页面是否刷新?或者你想通过AJAX发送数据? JSON并没有真正进入这个,因为你只是发送一个字符串... – Juned

+0

没有不好,当页面刷新因为可能导致短期数据丢失。只要解释了我就可以使用AJAX。 – Arkonsol

+0

或发布你的JS尝试,有人会帮助你。 – Juned

回答

0

-_- 似乎每个人都尝试过和当简单答案应该是这个代码时,这个问题变得复杂而且过度思考。

function caller(){ 
myData = "myTbl"; 
var xmlhttp = new XMLHttpRequest(); 
xmlhttp.open("GET", "bring.php?q="+myData,true); //sends myData to the php. You can change the GET to POST if ya want to be extra safe but either way, the php won't care anyways. 
xmlhttp.send(); 
} 

上面的代码是从所述客户端服务器和myData的数据发送到该服务器端PHP。

<?php 
$q = $_REQUEST['q']; //the receiver of the data. You can use explode() to separate them into pieces and turn it into a jigsaw puzzle if ya want. 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "Mydb"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} // how to connect is not important in my question but it is important for you to be able to connect to the database. 

// now for the important stuff 

$sql = "SELECT * FROM ".$q." "; // I had to extend it with a space because sometimes it's misunderstood. 
$result = mysqli_query($conn, $sql) or die("Error in Selecting " . mysqli_error($conn)); 
$outp = array(); 

while($row=mysqli_fetch_assoc($result)) 
{ 
    $outp[] = $row; 
} 
    echo json_encode($outp); //sends the data gathered from the database table back to the client as a JSON file, and you are done. 
?> 

我耗费了6个小时的时间学习了很多上网冲浪和试验和错误。

而且就像我说的;我是JSON的新手,所以我很了解我不懂AJAX。就像AT ALL。

对不起,如果亚认为我听起来像一个白痴,但是让我们面对它,几乎我们所有的人在一个点是笨蛋而努力学习编程。所以,我很惊讶为什么有些人在这里显得粗鲁无礼。谢谢。不知何故,我并不感到惊讶,他们也喜欢这里。使我的互联网社交生活变得更无聊。

因此,下一次

,帮我一个忙,而不是被完全粗鲁,只是回答这个问题,如果你有一个。

+0

就这么你知道,你在这里有一个基本的AJAX请求。 AJAX有一些有趣的跨浏览器问题,所以建议使用库来解决这个问题。 [Jquery](http://learn.jquery.com/ajax/)是一个受欢迎的选项,但它不是唯一的选项。另请参阅https://www.sitepoint.com/introduction-jquery-shorthand-ajax-methods/和https://www.sitepoint.com/use-jquerys-ajax-function/ –

+0

其实,我试图研究它,但我不知何故,总会有一些致命的错误。尝试了你发给我的链接(实际上我甚至在发布这个问题之前)。就像我在我的问题中所说的那样,我总是会遇到两个错误。我必须找出为什么它不适用于这些方法。这就是我问的原因。这只是我的代码的一部分,因为我不得不使用更多的代码来完成这个过程。我认为我的WAMP服务器出了问题。地狱它甚至不会运行,如果我没有互联网连接。 0_0是的,我尝试了Uni&Rei方法。还是一样。 – Arkonsol

0

可以使AJAX调用形式的客户端与服务器文件,可以用get方法

// Using the core $.ajax() method 
$.ajax({ 

    // The URL for the request 
    url: "path of your php file", 

    // The data to send (will be converted to a query string) 
    data: { 
     id: 123 
    }, 

    // Whether this is a POST or GET request 
    type: "GET", 

    // The type of data we expect back 
    dataType : "json", 
}) 
    // Code to run if the request succeeds (is done); 
    // The response is passed to the function 
    .done(function(json) { 
    }) 
    // Code to run if the request fails; the raw request and 
    // status codes are passed to the function 
    .fail(function(xhr, status, errorThrown) { 
    alert("Sorry, there was a problem!"); 

    }) 
    // Code to run regardless of success or failure; 
    .always(function(xhr, status) { 
    alert("The request is complete!"); 
    }) 
+2

注意到这需要作为jQuery的依赖......没有提到 – charlietfl

+0

其实我已经基本掌握使用JSON我的问题是只发送到PHP位从PHP位接收。 – Arkonsol

-2

,因为它是无法通过HTTP传输的目标发送数据。你需要把它改造成一个字符串,你可以把你的HTTP-POST请求的身体:

try { 
    var jsonString = JSON.stringify(anyJsonObject); 
    //send it to the server 
} catch(ex) { 
    //handle error if anyJsonObject wasn't a valid JSON object. Remember: Not every JS object is a JSON object too. 
} 

相反的方式是:

try { 
    var jsonObject = JSON.parse(anyJsonString); 
} catch(ex) { 
    //handle error if anyJsonString was malformed 
} 
+0

我已经知道该怎么做,这不是一个真正的回答我的问题 – Arkonsol

+0

哦,我回答我的问题。这将有助于清除你的想法*“你不能通过HTTP传输对象,因为它是*部分。你可以使用'$ _REQUEST'和一些'xmlhttp'帮助。 – Arkonsol

相关问题