简体   繁体   中英

500 internal error with Ajax

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.

You mixed JS and PHP syntax up, in sms.php file you have syntax errors.

  1. dollar sign before smtp
  2. idOne instead of $_POST['idOne']
  3. idTwo instead of $_POST['idTwo']
  4. additional bracket at the end of SQL query
  5. line 11, again, 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

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'];

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.

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>";    
        }
      }
    }
 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM