简体   繁体   中英

getting number from php file with jquery ajax

I'm new to AJAX and jQuery. I'm trying to pass a number from unrate.php to be used as checkVal (as shown below). The file does a bunch of stuff but it only echos the number. When I add a alert(checkVal) it shows a invalid character and than the number I want. (I just want the number)...

ajax handler:

$.get("unrate.php?numb="+ID, function(checkVal){
  if (checkVal == 1) {
    number.innerHTML = addNumb + 1;
  } else { 
    number.innerHTML = addNumb - 1;
  }
});

unrate.php:

<?php
$uNum = $_SESSION['userNum'];
$ider = $_GET['numb'];
$sql = mysql_query("SELECT * FROM ratecheck WHERE ID =".$ider);
$checkRay = mysql_fetch_array($sql);
$checkVal = $checkRay[$uNum];

$sqlZ = mysql_query("UPDATE ratecheck SET `".$uNum."`=0 WHERE ID=".$ider)
or die(mysql_error());

    $sqlB = mysql_query("SELECT * FROM sources WHERE ID =".$ider);
    $sourceRay = mysql_fetch_array($sqlB);
    $newRC = $sourceRay['ratecount'] - 1;

    mysql_query("UPDATE sources SET ratecount =".$newRC." WHERE ID =".$ider)
    or die(mysql_error());

if ($checkVal > 1)
    {   
    $newpts = $sourceRay['points'] - 1; 
    $userEmail = $sourceRay['user'];

    mysql_query("UPDATE sources SET points =".$newpts." WHERE ID =".$ider)
    or die(mysql_error());  

        if ($_SESSION['userName']) 
        {
            $findUser = mysql_query("SELECT * FROM users WHERE email LIKE '".$userEmail."'") or mysql_error();
            $currentRate = mysql_fetch_array($findUser);
            $newrating = $currentRate['rating'] - 1;
            mysql_query("UPDATE users SET rating =".$newrating." WHERE email LIKE '".$userEmail."'")
            or mysql_error();      

        }
        else
        {
            die('ERROR');
        }
    }
else 
    {
    $newpts = $sourceRay['points'] + 1; 
    $userEmail = $sourceRay['user'];


    mysql_query("UPDATE sources SET points =".$newpts." WHERE ID =".$ider)
    or die(mysql_error());

        if ($_SESSION['userName']) 
        {
            $findUser = mysql_query("SELECT * FROM users WHERE email LIKE '".$userEmail."'") or mysql_error();
            $currentRate = mysql_fetch_array($findUser);
            $newrating = $currentRate['rating'] + 1;
            mysql_query("UPDATE users SET rating =".$newrating." WHERE email LIKE '".$userEmail."'")
            or mysql_error();      

        }
        else
        {
            die('ERROR');
        }
    }
echo $checkVal;
mysql_close(); 
?>

Extra characters at the beginning or end of your output are something you occasionally run into with php. I greatly endorse the comment that suggests looking at the raw output from the server. You might also want to think about these possibilities:

Invisible characters at the beginning or end of your script file. Use a text editor that will show you hidden characters (even a hex editor) and see if there are any. Also, you don't have to end your php script with ?> if you're not doing anything else past it. You can just leave it open, as that will prevent characters showing up at the end.

Check the character encoding that your script has. This might not be the solution, but some time ago I had a similar situation that went away when I changed the encoding to UTF8 without Byte-Order Mark. Try doing the same thing and see if that fixes it

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