Hi everybody, I hope somebody can help me, because I am very confused.
I have problem with load sequence I can not execute search request or any other request with this script ?
sorry about bad english:blush:
<?php
/********** CONSTANTS *************/
define("HOST", "xxxx");
define("USER", "xxxx");
define("PASSWORD", "xxxx");
define("DB", "xxxx");
/********** CONSTANTS *************/
function connect($db, $user, $password){
$link = @mysql_connect($db, $user, $password);
if (!$link)
die("Could not connect: ".mysql_error());
else{
$db = mysql_select_db(DB);
if(!$db)
die("Could not select database: ".mysql_error());
else return $link;
}
}
function getContent($link, $num){
$res = @mysql_query("SELECT * FROM TABLE ORDER BY position desc limit ".$num, $link);
if(!$res)
die("Error: ".mysql_error());
else
return $res;
}
function getSearch($link, $num){
$research = @mysql_query("SELECT * FROM TABLE WHERE name='$search' ORDER BY position desc limit ".$num, $link);
if(!$research)
die("Error: ".mysql_error());
else
return $research;
}
function insertMessage($user, $message){
$query = sprintf("INSERT INTO TABLE(user, message) VALUES('%s', '%s');", mysql_real_escape_string(strip_tags($user)), mysql_real_escape_string(strip_tags($message)));
$res = @mysql_query($query);
if(!$res)
die("Error: ".mysql_error());
else
return $research;
}
/******************************
MANAGE REQUESTS
/******************************/
if(!$_POST['action']){
//We are redirecting people to our App page if they try to enter in our app.php
header ("Location: index.html");
}
else{
$link = connect(HOST, USER, PASSWORD);
switch($_POST['action']){
case "update":
$res = getContent($link, 15);
while($row = mysql_fetch_array($res)){
$result .= "<div id=container'>".$row['3']." - ".$row['11']."</div>";
}
echo $result;
break;
case "search":
$research = getSearch($link, 15);
while($row = mysql_fetch_array($research)){
$resultsearch .= "blablabla";
}
echo $resultsearch;
break;
case "insert":
echo insertMessage($_POST['nick'], $_POST['message']);
break;
}
mysql_close($link);
}
?>
JS
$(document).ready(function(){
//global vars
var inputUser = $("#nick");
var inputMessage = $("#message");
var loading = $("#loading");
var messageList = $(".content"); // .content > ul
//functions
function updateShoutbox(){
//just for the fade effect
messageList.hide();
loading.fadeIn();
//send the post to app.php
$.ajax({
type: "POST", url: "app.php", data: "action=update",
complete: function(data){
loading.fadeOut();
messageList.html(data.responseText);
messageList.fadeIn(2000);
}
});
}
function searchShoutbox(){
//just for the fade effect
messageList.hide();
loading.fadeIn();
//send the post to app.php
$.ajax({
type: "POST", url: "app.php", data: "action=search",
complete: function(data){
loading.fadeOut();
messageList.html(data.responseText);
messageList.fadeIn(2000);
}
});
}
//check if all fields are filled
function checkForm(){
if(inputUser.attr("value") && inputMessage.attr("value"))
return true;
else
return false;
}
//Load for the first time the app data
updateShoutbox();
//on submit event
$("#form").submit(function(){
if(checkForm()){
var nick = inputUser.attr("value");
var message = inputMessage.attr("value");
//we deactivate submit button while sending
$("#send").attr({ disabled:true, value:"Sending..." });
$("#send").blur();
//send the post to app.php
$.ajax({
type: "POST", url: "app.php", data: "action=insert&nick=" + nick + "&message=" + message,
complete: function(data){
messageList.html(data.responseText);
updateShoutbox();
//reactivate the send button
$("#send").attr({ disabled:false, value:"Shout it!" });
}
});
}
else alert("Please fill all fields!");
//we prevent the refresh of the page after submitting the form
return false;
});
});