1

I checked a multiples threads on the same kind of errors in stackoverflow but I could not find an answer to my query so I am posting this question.

I am using jquery Ajax to pass a variable to a PHP and get the results back but I get the below error

Notice: Undefined variable: captain in C:\fpl\checkCaptain.php on line 22 null

Below are the jQuery and PHP script that I am using.

jQuery AJAX:

$('#outPlayer').change(function(){
$.ajax({
type: "GET",

url: "checkCaptain.php",
data: 'q='+$('#OutPlayer').val(),
success: function(msg){
if(msg == 1)
{ $('#test').html(msg); }
else
{ $('#test').html(msg); }
}

PHP Page:

include_once("includes/mySqlConnect.php");

$query = sprintf("SELECT MAX(gameweek) AS gameweek FROM gameweek");
$sql = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($sql))
{
    $gameweek = $row['gameweek'];
}

$outPlayer = $_GET['q'];

$query1 = sprintf("SELECT captain FROM gameweek WHERE gameweek = '%s' AND player = '%s'", $gameweek, $outPlayer);
$sql1 = mysql_query($query1) or die(mysql_error());

while($row1 = mysql_fetch_array($sql1))
{
    $captain = $row1['captain'];
}

echo $captain;

I would really appreciate a quick help here. Thanks in advance!! Apologies if there are silly mistakes. I am new to Ajax and PHP :)

Dragan Kidovic
  • 355
  • 2
  • 3
  • 9

2 Answers2

2

This error occured since $captain is defined inside the while. If this query yielded 0 rows, then your $captain will be undefined thus the error. You should always initialize your variables.

$outPlayer = $_GET['q'];

$query1 = sprintf("SELECT captain FROM gameweek WHERE gameweek = '%s' AND player = '%s'", $gameweek, $outPlayer);
$sql1 = mysql_query($query1) or die(mysql_error());

$captain = 0; // initialize always!

if(mysql_num_rows($sql1) > 0) {
    while($row1 = mysql_fetch_array($sql1)) {
        $captain = $row1['captain'];
    }   
}

echo $captain;
exit;

Obligatory Note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • Thanks for the inputs, However before posting this question, I had tried initializing the variable captain but it did not solve the issue. – Dragan Kidovic Oct 18 '14 at 07:17
  • Thanks for providing those articles on tutorials on PDO and Mysqli, I will use them in all my future assigments.. Lot to learn :) – Dragan Kidovic Oct 18 '14 at 07:19
  • @RoshanRaikar yes you should adapt to this changes now, don't wait for your codes to be attacked by an sql injection. Use prepared statements, actually they are quite easy to follow since the tutorial gives you the deprecated mysql CRUD, and shows the newer PDO counterpart – Kevin Oct 18 '14 at 07:22
-1

May be you have to change

data: 'q='+$('#OutPlayer').val(),

to this value like

$('#outPlayer').change(function(){
    var selected_val = $(this).val();
    $.ajax({
    type: "GET",

    url: "checkCaptain.php",
    data: 'q='+selected_val,
    success: function(msg){
    if(msg == 1)
    { $('#test').html(msg); }
    else
    { $('#test').html(msg); }
 });
GautamD31
  • 28,552
  • 10
  • 64
  • 85
  • @RGraham are you serious..?can you once check – GautamD31 Oct 18 '14 at 07:04
  • ok @RGraham I have changed it, but it will not be the problem at all' – GautamD31 Oct 18 '14 at 07:05
  • 2
    You're correct, I didn't realise `$.ajax` changed the context of `this` like that. Interesting. Nevertheless, it'll always return the same value as the OP question. – CodingIntrigue Oct 18 '14 at 07:07
  • Any reason to down it? – GautamD31 Oct 18 '14 at 07:16
  • Thanks a lot for this quick fix. Yes, changing the data: 'q='+$('#OutPlayer').val(), to data: 'q='+$(this).val(), did the trick – Dragan Kidovic Oct 18 '14 at 07:17
  • 1
    I didn't downvote it, but it could be because it's so misleading. Notice how the original question says `'q='+$('#OutPlayer').val()`, but `$("#outPlayer")` for the change handler? That's the real issue, not that they need to use `$(this)` which just happens to refer to the same object. – CodingIntrigue Oct 18 '14 at 07:20
  • 1
    I don't know PHP or AJAX but don't forget that when your lambda function get executed, things like `$('#OutPlayer')` and `this` are probably not what you think. You should define the value of `data` before calling your lambda function. As to your error message, `$captain` is undefined probably because the second query is empty (the resultset has no row). You can easily test this by giving a default value to `$outPlayer` when `$_GET['q']` is Null or empty. You should also give a default value to `$captain`. It's a got habit to always initialise your variables and check what you get. – SylvainL Oct 18 '14 at 07:20
  • @RGraham it is refering the same object, may be he done some typo that's it.And second SylvainL yes you are right.it is good habit to check with static values, but here it is not the issue with the checking, it is issue with the value that he is sending through the ajax – GautamD31 Oct 18 '14 at 07:23
  • @Gautam3164 You're right, it **is** a typo. I've voted to close the question as such. – CodingIntrigue Oct 18 '14 at 07:24