user2465936
user2465936

Reputation: 1040

Ajax pass value to other php file and use the value in if statement

<td id='account_type'>Asset</td>

then ajax

$.ajax({
type: 'POST',
url: '__popup-window_ajax.php',
data: { 'AccountType' : $('#account_type').text() },
});

then php

print_r($_POST['AccountType']);

see word Asset exactly as here <td id='account_type'>Asset</td>

but

if (($_POST['AccountType']) == 'Asset') {
echo 'Yes, Asset';
}

echo nothing

What is wrong?

For example tried

$account_type = 'Asset';
if ( $account_type == 'Asset' ) {
echo 'Yes, Asset';
}

and works... Does ajax changes values somehow?

Solution Thanks to @Ankit Pokhrel. My stupid negligence. After word Asset there was blank space. trim helped.

Upvotes: 1

Views: 72

Answers (2)

Mitya
Mitya

Reputation: 34556

This can only be a whitespace issue. It has nothing to do with the treatment of $_POST or whether or not your data property names are encased in quotes. Trim the value on either side of the transaction.

JS:

data: {AccountType: $('#account_type').text().replace(/^\s|\s$/g, '')},

PHP

trim($_POST['AccountType'])

Upvotes: 4

Garytje
Garytje

Reputation: 874

Try:

if (isset($_POST['AccountType']) && stristr($_POST['AccountType'],'Asset')){   
    //extra content is sent within AccountType variable, check your output sourcecode. 
}

// or

if (isset($_POST['AccountType']) && trim($_POST['AccountType']) =='Asset'){    
//you've got whitespaces in AccountType variable 
}

Upvotes: 5

Related Questions