0
我正在嘗試使用json,出於某種原因,直到我實現任何類型的mysql時,這都可以正常工作。我從教程中偷了這個。我嘗試使用準備好的語句和基本的mysqli查詢;但是,當我這樣做時,不會返回任何數據。使用mysql查詢時JSON/PHP不工作
這裏是jQuery的:
<script type="text/javascript">
$("document").ready(function(){
$(".sendText").submit(function(){
$(".errors").html("");
$(".success").html("");
var data = {
"action": "test"
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "POST",
dataType: "json",
url: "jsonpost.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
if(data["success"]=="yes") {
$(".success").html("Message Sent!");
$(".formContainer").html("" + data["json"] + "");
} else {
$(".errors").html("" + data["errors"] + "");
}
}
});
return false;
});
});
</script>
這裏是PHP:
<?php
header('Content-Type: application/json');
require "../database/db.php";
if (is_ajax()) {
if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
$action = $_POST["action"];
switch($action) { //Switch case for value of action
case "test": test_function(); break;
}
}
}
//Function to check if the request is an AJAX request
function is_ajax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
function test_function(){
$return = $_POST; //to reference post
$phone=$return[phone];
$yphone=$return[yphone];
$alert=$return[alert];
function generateRandomString($length) {
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
$confirmCode=generateRandomString(6);
//validate sender phone number
if(empty($phone)){
$c="0"; $empty="<li>Please enter their phone number!</li>";
}
//see if content is 140 characters or less
if(strlen($content)>140){
$c="0"; $lerror="<li>Your message must be 140 characters or less in length!</li>";
}
/* IP Address Info */
$ip=$_SERVER["REMOTE_ADDR"]; //get sender IP address
$ip=hash('sha256', $ip); //hash IP address
/* END IP Info */
$etime=time()-18000; //5 hours ago from current time
$time=time(); //current time
/*This makes the code fail */
$result = mysqli_query($con,"SELECT * FROM messages WHERE ip='$ip' AND phone='$phone' AND time BETWEEN $etime AND $time"); //check if ip has sent more than 5 messages to phone number in less than 5 hrs.
$totaltexts=mysqli_num_rows($result);
if($totaltexts>4){
$c="0"; $exceedmessage="<li>You have sent $phone the limit of 5 messages per 5 hours. Please try again later.</li>";
}
/* ------------------------- */
if($c=="0"){
$return["success"]="no";
$return["errors"]="$empty $phoneerror $hooderror $yphoneerror $lerror $exceedmessage $exceedmessagea $exceedmessage2";
} else {
$return["success"]="yes";
}
$return["json"] = json_encode($return);
echo json_encode($return);
}
?>
要求 「../database/db.php」;在php代碼 – pkjayk 2014-09-21 22:14:38
的第三行,並在db.php是$ con聲明爲全局? – manishie 2014-09-21 22:17:49
我將$ con聲明爲全局的,並且工作正常。 – pkjayk 2014-09-24 03:31:46