Skip to content
Advertisement

Ajax gets as result whole page from php

I am trying to call php function using ajax. Ajax is called from php with onclick function in button. But instead of value, that the called function add_player returns, i get whole page exec.php. Thank you for your help.

index.php

echo '<script type="text/javascript" src="dragon_javascript.js"></script>';
echo '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>';
echo '<input type="text" name="jmeno" id="jm" value="Zadej jmeno uzivatele">';
echo '<input type="text" name="prijmeni" id="pr" value="Zadej celkove body uzivatele">';
echo '<button onclick="javascript:add_user()">pridej uzivatele</button>';

javascript

function  add_user() {
    var player_name =  document.getElementById('jm').value;
    var overall_points =  document.getElementById('pr').value;
    $.ajax({
        url:"exec.php?action=add_player",
        type:'POST',
        datatype:'json',
        data:{
            player_name:player_name,
            overall_points:overall_points
        },
    success: function(result){
        
        if(result=='success') alert('it worked');
        else alert('did not work');
        alert(result);

    },
    error: function()
    {
        alert('did not workaaa');

    }
    });
}
 

exec.php

function add_player($player_name, $overall_points)
{
// connect to database
      dibi::connect([ 
    'driver'   => 'mysqli',
    'host'     => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'draci_sipka',
    'charset'  => 'utf8',
  ]);
    try 
    {
      dibi::query('INSERT INTO player VALUES(1,Null,%s,%i)',$player_name,intval($overall_points));
    } 
  catch (DibiException $e) 
  {
      return 'mistake';
  }
  return 'success';
     
}

Advertisement

Answer

I assume that somewhere, but not shown, index.php has …

<?php
...
?>

… surrounding the echo statements. If not, make it so.

If you are returning the entire code of exec.php as your result, that suggests that you do not have your code enclosed within <?php ... ?> tags. That is problem number one. The second problem is how you are retrieving the actual parameters passed to the script. The third problem is how you are trying to send back the results.

exec.php

<?php

/* get the two passed parameters: */
$add_player = $_REQUEST['player_name']; // works for get or post requests (or $_POST['player_name'], which is post-specific
$overall_point = $_REQUEST['overall_point];

/* do computations: */
// actual computations omitted here
$result = 'success'; // or possibly 'mistake'

/* send back the result */
header('Content-Type: text/plain'); // describe the mime-type of the result
echo $result; // write out the result

The final ?> may be omitted. You can also test whether the expected parameters have been passed with:

if (!isset($_REQUEST['player_name'])) {
    // error code
}

Ultimately it is what your script writes out that is considered the “return value” for the Ajax request. The PHP interpreter outputs whatever is not within <?php ... ?> tags so when you omitted those tags, your source program itself was written out (with a default mime-type) and considered the return value.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement