0

I want the $delivery in the top input to change value depending on the value on another variable I'm using.

I'm just not sure how to place another if inside the current if and how to match up the value="0.00" so it all works together

<form action="" method="post" onclick="this.submit()">
    <input <?php if ($delivery=='0.00'){ echo 'checked="checked"';} ?> type="radio" value="0.00" name="delivery">&nbsp; Economy - up to 4 working days<br>
    <input <?php if ($delivery=='4.99'){ echo 'checked="checked"';} ?> type="radio" value="4.99" name="delivery">&nbsp; Express - next day <br>
    <input <?php if ($delivery=='9.99'){ echo 'checked="checked"';} ?> type="radio" value="9.99" name="delivery">&nbsp; Saturday<br>
</form>

Thanks.

What i ended up doing was this:

if ($pricetotal <= 50.00) {
$amount1 = 3.99;
}
elseif ($pricetotal >= 50.01) {
$amount1 = 0.00;
}

Then in my form changing it to this:

<form action="" method="post" onclick="this.submit()">
<input <?php if ($delivery<='3.99'){ echo 'checked="checked"';} ?> type="radio" value="       <?php echo htmlspecialchars($amount1); ?>" name="delivery">&nbsp; Economy - up to 4 working days<br>
<input <?php if ($delivery=='4.99'){ echo 'checked="checked"';} ?> type="radio" value="4.99" name="delivery">&nbsp; Express - next day <br>
<input <?php if ($delivery=='9.99'){ echo 'checked="checked"';} ?> type="radio" value="9.99" name="delivery">&nbsp; Saturday<br>

I might not have explained exactly what i needed as i thought there would be a simple fix, but i wanted the top inputs value do change depending on $pricetotal and i managed to get it working.

Thanks for the help.

2
  • Couldn't use a flag and a variable inside the value tag, instead of hardcoding it. Commented Aug 12, 2014 at 13:34
  • It sounds like that you need AJAX or at least some plain JavaScript to do this. PHP is server-side and therefore can just change values on a HTML page after reloading (or via AJAX)... Commented Aug 12, 2014 at 13:36

2 Answers 2

1

I would do something like this:

$deliveryEconomy = '';
$deliveryExpress = '';
$deliverySaturday = '';

switch($delivery)
{
   case 0.00:
   $deliveryEconomy = 'checked="checked"';
   break;

   case 4.99:
   $deliveryExpress = 'checked="checked"';
   break;

   case 9.99:
   $deliverySaturday = 'checked="checked"';
   break;
}

<form action="" method="post" onclick="this.submit()">
<?php 
echo "<input $deliveryEcononomy type='radio' value='0.00' name='delivery'>&nbsp; Economy - up to 4 working days<br>";
echo "<input $deliveryExpress type='radio' value='4.99' name='delivery'>&nbsp; Express - next day <br>";
echo "<input $deliverySaturday type='radio' value='9.99' name='delivery'>&nbsp; Saturday<br>";
?>
</form>
Sign up to request clarification or add additional context in comments.

3 Comments

This would have perfect if i didnt need the value to change, i tried using this with the different values but couldn't figure it out :(
You mean the delivery costs change? You can use an if statement (instead of a switch) and use a cost range, if that would be better. If you elaborate, I'd be glad to help.
I've put my working code in my question, as i say i didn't really put down enough info as i thought there would be an simple answer, in the end as you can see i did use a if statement and its working great! sorry about the mix up and thanks for the help!!
0

Normally you would run logic before this, but if you are looking for a quick and dirty mini-if statement, something like this might work for you:

if ($delivery==($boolVar ? '0.00' : 'something else')){ echo 'checked="checked"';}

These statements have the same result:

if($condition) {
    $var = $statement_1;
} else {
    $var = $statement_2;
}

$var = $condition ? $statement_1 : $statement_2;

Don't overuse it however, otherwise you may be tempted to try:

$var = $condition_1 ? $statement_1 : ($condition_2 ? $statement_2 : $statement_3);

Messy.

2 Comments

Hey, Ive never seen this before what exactly does ($boolVar ? '0.00' : 'something else') mean? does the : count as an or? cant see anything else online :)
Its a shorthand if statement: condition ? then : else, meaning if condition then statement else statement. The either statement will be the return value, or the executing statement if the return is not stored.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.