If statement in onclick event

It always says “nothing in text box”

<input type="text" class="formSmallTextbox" name="a_number" size="4" value="<?="$a_number"?>">
<input type="submit" name="submit" class="formTextbox" value="Submit" onClick="<?php
	if ($a_number >= '1'){
	echo "return confirm('something in text box')\\">";}				else{
	echo "return confirm('nothing in text box')\\">";}
	?>

Well, then that means $qty_oak is always < 1 and you should find out why that is. I also suspect you may be confusing client and server side scripting. There’s no point in using a javascript confirm if the logic behind it is handled before the page has even been presented to the user.

It just occurred to me that when the page loads the text box a_number is always empty thats why it echos “nothing in text box”. How do I get php to recognize that something was put in the text box after the page was loaded?

Yes, you are confusing client-side and server-side scripting. This:


<input type="submit" name="submit" class="formTextbox" value="Submit" onClick="<?php

    if ($qty_oak >= '1'){

    echo "return confirm('something in text box')\\">";}                else{

    echo "return confirm('nothing in text box')\\">";}

    ?>

will output the following static HTML:


<input type="submit" name="submit" class="formTextbox" value="Submit" onClick="return confirm('something in text box')">

if your PHP variable $qty_oak is >= 1. Otherwise it will return a confirm prompt with the other text, regardless of what is in the text input.

PHP has no control over the page once it has loaded. As soon as PHP sends the page to the user, that’s its job done and anything dynamic must be solely handled by javascript (unless you’re using AJAX). So the answer to How do I get php to recognize that something was put in the text box after the page was loaded? is “you can’t”. That’s javascript’s job. I think what you want is to do this:


<input type="text" class="formSmallTextbox" name="a_number" size="4" value="">

<input type="submit" name="submit" class="formTextbox" value="Submit" onClick="if (document.getElementsByName('a_number')[0].value == '') return confirm('nothing in text box'); else return confirm('something in textbox');">

That, however, is horribly messy and you should consider using unobtrusive javascript.

thanks raffles that works

i’ll look into unobtrusive javascript