Conditional Operator Help

I am not sure of the terms I am looking for to solve an issue but I want the following:

This is in a form

If user picks numbers ranging from 1000-2000 from drop-down select field called “worth”

then the next drop-down field called “amount” must be less than “worth”
Although the select options here will also have 1000-2000. The user has to choose 1000-1800 as it must be 10% or less of “worth”

If you could point me in the right direction I’d appreciate it.

Thanks

A little jquery should do the job of POSTing first selection to a new file called amount-request.php, which will take POST[‘worth’] and build the options for the second selection.

amount-request.php

<?php    
    if(isset($_POST['worth'])){
		// Set $max variable to 90% of worth
		$max = ($_POST['worth']*.90 >= 1000 ? $_POST['worth']*.90 : 1000);
	    // Build the options for the second select box incremented by 10
	    foreach(range(1000,$max,10) as $a){ echo '<option value="'.$a.'">'.$a.'</option>'; }
    }  
?>

Here’s a working sample form.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
</head>
<body>   
    <form action="" method="post">
    <select id="worth" name="worth">
    <?php
    // Build the options 1000 through 2000 for the first select box incremented by 50
    foreach(range(1000,2000,50) as $w){ echo '<option value="'.$w.'">'.$w.'</option>'; }
    ?>
    </select>
    <select id="amount" name="amount">
    </select>
    <input type="submit" name="submit" value="Submit">
    </form>
	    
	<script type='text/javascript'>
    $('#worth').change(function(){ 
    var worth = $(this).val(); 
    $.post('amount-request.php', {worth:worth}, function(data){ 
    $('#amount').html(data); 
    });
    });
    </script>
</body>
</html>

I just realized your first selection is a two number range. I’ll look into that.

10% or less of “worth”

I just realized this will only work up to 10000 as 10% of 10000 is 1000 and then you have no range of options., e.g. 9000-10000. Sure you want top amount to be 10% less?

Here’s a new version of amount-request.php, giving you three options for max value. The one used here is 100 off max value.

<?php
    if(isset($_POST['worth'])){
		$range = explode('-',$_POST['worth']);
		$min = $range[0];
		//$max = $range[1];  // Full max value
		//$max = $range[1]*.90;  // Minus 10% only works up to 10000		
		$max = $range[1]-100;	 // Just subtracting 100 from top value
		
	    // Build the options for the second select box incremented by 100
	    foreach(range($min,$max,100) as $a){
			echo '<option value="'.$a.'">'.$a.'</option>';
		}
    }
?>

And new sample form.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
</head>
<body>
    <form action="" method="post">
    <select id="worth" name="worth">
    <?php
	$toprange = 19000;  //20000 minus 1000
    foreach(range(1000,$toprange,1000) as $w){
		$t = $w + 1000;	
		echo '<option value="'.$w.'-'.$t.'">'.$w.'-'.$t.'</option>';
	}
    ?>
    </select>
    <select id="amount" name="amount">
    </select>
    <input type="submit" name="submit" value="Submit">
    </form>
	
	<script type='text/javascript'>
    $('#worth').change(function(){
    var worth = $(this).val();
    $.post('amount-request.php', {worth:worth}, function(data){
    $('#amount').html(data);
    });
    });
    </script>
</body>
</html>

Hey Drummin, this would work great except here’s my dilemma.

It is a form-mail and I am getting a bot that fills-in/selects every input/select fields. After the bot selected "worth’, then selected “amount” usually the same or higher of “worth” it would be an amount that is impossible. So, if it auto-populate to 90% of “worth” the bot will still select any arbitrary number and get through.
What I’ve tried so far: hidden css fields, captcha, time check, banned IP.
So, I am confident that the bot can’t figure out 90% or less of the “worth” field, IF it chooses 100% or higher, then it will simply not submit.

It would be pretty easy to just compare worth and amount (90%) on POST if that’s what you want to do.

You might try breaking your form into two parts. The first POST would save those values to session and if session values are present after a page reload, the second part of the form would show where they would fill in the rest. I don’t think BOT’s save sessions. If it’s a human spammer or hacker do a good check for “tags” in any form fields. If one is found, get the IP or FORWARDED_FOR IP address and add it to your banned list.

if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARTDED_FOR'] != '') {
   $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
   $ip_address = $_SERVER['REMOTE_ADDR'];
}

Good Luck!

A little bit off topic, but it seems that conditional/ternary operator is malfunctioning in PHP 5.4.23 and 5.4.24 on litespeed servers. A bug issue has been submitted on php.net, although a solution has yet to be found. Anyone experiencing this problem? And does it affect PHP 5.5?

https://bugs.php.net/bug.php?id=66524