Link to home
Create AccountLog in
Avatar of TLN_CANADA
TLN_CANADAFlag for Afghanistan

asked on

Alter printing ajax variable so it includes if statement

Hi everyone,

Here is an ajax statement that prints the variable reply[0]

$('#myH2').html('Your current value is ' + reply[0]);

I would like to alter what is displayed in the H2 area depending on whether or not the value of the variable is empty.

Here is how I had it working in PHP previously (reply[0] is called $row['question'] here)

<h2><?php
if (!empty($row['question']))
{
    echo 'Your current value is...' . $row['question'] . ' <img src="change_question_little_button.jpg" alt="Pulpit rock" width="60" height="50">';
}
else
{
    echo '<img src="change_question_little_button.jpg" alt="Pulpit rock" width="60" height="50">';
} 
?></h2>

Open in new window


Thanks for your help,

D
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

it depends. does the call return an empty string, or does it just not return anything?

if it's an empty string you can do this:
if (reply[0] != "") {
    $('#myH2').html('Your current value is ' + reply[0]);
} else {
    $('#myH2').html('something else');
}

Open in new window


if it's null or undefined you can do this:
if (reply[0]) {
    $('#myH2').html('Your current value is ' + reply[0]);
} else {
    $('#myH2').html('something else');
}

Open in new window

or you can check for both:
if (reply[0] != "" || reply[0]) {
    $('#myH2').html('Your current value is ' + reply[0]);
} else {
    $('#myH2').html('something else');
}

Open in new window

Avatar of TLN_CANADA

ASKER

Thanks  kozaiwaniec,

I tried this one:

if (reply[0] != "" || reply[0]) {
    $('#myH2').html('Your current value is ' + reply[0]);
} else {
    $('#myH2').html('something else');
}

Open in new window


the field in the table is definitely blank at the moment but it is still displaying "Your current value is   ".

Thanks for your help,

Derek
Avatar of Rainer Jeschor
Hi,

I would try it the other way around:
if (!reply[0] || reply[0] == null || reply[0] === "") {
    $('#myH2').html('something else');
} else {
    $('#myH2').html('Your current value is ' + reply[0]);
}

Open in new window


HTH
Rainer
Hi Rainer,

It's still coming up with "Your current value is    " even though there is no value in the database field.

Cheers,

D
Could you add a - before and after the output?
I assume that there is a value in there (could be a space)
$('#myH2').html('Your current value is -' + reply[0]+'-');
If there is a value in the database field, it displays correctly. It's the other part of the statement it doesn't seem response to.
ASKER CERTIFIED SOLUTION
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
This works! Thank you.
Hi guys,

I have a follow on from this question and if either of you could take a look at it, that would be greatly appreciated.

https://www.experts-exchange.com/questions/28034948/Updating-multiple-values-via-ajax.html

Thanks,

D