Reputation: 105
I'm having a problem when I'm doing a web-chat page with php and ajax. The problem is that when see the webpage in my browser, in the console apears this mesage:
POST http://subdomain.domain.com/ajax/sms.php 500 (Internal Server Error)
x.ajaxTransport.send @ jquery.min.js:6
x.extend.ajax @jquery.min.js:6
doAjax @ funcionesChat.js:22onclick @ chat:50
And I have no idea why apears that and what I've done wrong in the page...
In my html I have the folowing code:
<button type="button" onclick="doAjax(2)">a</button>
<div class="chat">
<div class="boxChat" id = "sms">
</div>
</div>
In my ajaxfunction.js
is the following code:
function doAjax(idOne, idTwo) {
$.ajax({
type: 'POST',
url: 'ajax/sms.php',
data: {idOne: idOne, idTwo: idTwo},
dataType: 'text',
success: function (data) {
$('#sms').html(data);
}
}
And the sms.php
contains the following code:
<?php
$db = new PDO('mysql:host=localhost;dbname=XXXX;charset=utf8',
'user', 'password');
stmt = $db->query("SELECT * FROM sms WHERE sender = ".idOne."
AND reciver = ".idTwo.")");
$smss = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($smss as $sms) {
if ($sms["sender"] == idOne){
echo "<p style='text-align: right;'>".$sms["mesage"]."</p>";
}else{
echo "<p style='color: green;''>".$sms["mesage"]."</p>";
}
}
?>
Some one can give me a hand? Thanks a lot in advance.
Upvotes: 1
Views: 699
Reputation: 2625
Use like this
<?php
if(!isset($_POST['idOne'])){
echo "Error! missing idOne";
} else if(!isset($_POST['idTwo'])) {
echo "Error! missing idTwo";
} else {
$idOne = $_POST['idOne'];
$idTwo = $_POST['idTwo'];
$db = new PDO('mysql:host=localhost;dbname=XXXX;charset=utf8',
'user', 'password');
$stmt = $db->query("SELECT * FROM sms WHERE sender = ".$idOne."
AND reciver = ".$idTwo);
$smss = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($smss as $sms) {
if ($sms["sender"] == $idOne){
echo "<p style='text-align: right;'>".$sms["mesage"]."</p>";
}else{
echo "<p style='color: green;''>".$sms["mesage"]."</p>";
}
}
}
?>
Upvotes: 0
Reputation: 27082
You mixed JS and PHP syntax up, in sms.php
file you have syntax errors.
smtp
idOne
instead of $_POST['idOne']
idTwo
instead of $_POST['idTwo']
idOne
instead of $_POST['idOne']
$stmt = $db->query("SELECT * FROM sms WHERE sender = ".$_POST['idOne']."
AND reciver = ".$_POST['idTwo']);
And line 11
if ($sms["sender"] == $_POST['idOne']){ // $_POST['idOne'] instead of idOne
Upvotes: 2
Reputation: 4299
A 500 is a server error. It means something broke badly on the server, so it's probably not your ajax, unless you are sending completely invalid data. One question - is "reciver" the correct field name in the DB? It's misspelled. If that is an incorrect column name in the database, that'll be your error right there.
Upvotes: 0
Reputation: 21759
I see a couple of typos here:
stmt = $db->query("SELECT * FROM sms WHERE sender = ".idOne."
AND reciver = ".idTwo.")");
should be:
$stmt = $db->query("SELECT * FROM sms WHERE sender = ".$idOne."
AND reciver = ".$idTwo.")");
And where is $idOne
and $idTwo
defined?
Maybe you are missing something like:
$idOne = $_POST['idOne'];
$idTwo = $_POST['idTwo'];
Upvotes: 0