Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why doesn't this PHP script work?

Status
Not open for further replies.

Athanasopolous

Programmer
Jun 25, 2005
40
US
Why doesn't this work?
Code:
<?php
    if ($_POST['textbox1'] != "")
		$_ENV['mytestzeststring'] = $_POST['textbox1'];
	print $_ENV['mytestzeststring'];
?>

<form name="registerform" id="registerform" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post">
	  <input type="text" name="testbox1"/>
	  <input type="submit" value="Click me!"/>

</form>
Wouldn't this present on the page before the form declaration whatever the user types in the test box?
 
looks to me like you have been using python a lot recently
PHP needs braces to enclode code blocks.

Also I am not too shrure about $_ENV & personaly I would test & set the variable using the ternery fuctions:

Code:
$variable= (isset($_POST['testbox']))? $_POST['testbox']:"";
[/code/]

it will avoid a lot of php warnings in your error log.


I do not Have A.D.D. im just easily, Hey look a Squirrel!
 
braces are not needed if the action immediately follows the condition and is on one line.

so
Code:
if($foo == $bar)
  echo 'oopsy daisy';
is fine.

the reason why this code never works as expected is because you have named your form control 'tesbox1' where as you are assigning the value of $_POST['te[red]x[/red]tbox1'] to the $_ENV variable. as no such field is submitted, a null value is assigned. When you echo the NULL value, nothing is output.
 
Opps I missed that jpadie
also I didnt know about the braces thing (I think I would prefer to keep them to avoid any confusion & allow for easy expansion)

I still prefer my solution of the ternery operator in this case, ( anotht technique I learnt here)


I do not Have A.D.D. im just easily, Hey look a Squirrel!
 
avoiding the braces is not a great idea for readability imo. the only time when I ever do it is like this

Code:
if ($foo == $bar) return true;

i.e. where the conditional is straightforward and halts the method. in all other cases I prefer to use the full alternative syntax
Code:
if ($foo == $bar):
  //do something
endif;
the reason being simply that since all control structures use braces in php, it can become difficult to work out which structure one is closing on any particular line. The alternative syntax does not solve that problem, but it sure makes it easier to spot where you go wrong.

Like you, I prefer the ternary operator to simple if ... then ... else structures. more compact and I believe that they execute faster. although i have no evidence for my belief.

however i read in the php manual that you should not stack ternary operators. I'm not sure I agree with that advice.
 
I agree nested braces can be a pain

This is why I love python for programming (not so hot for the web though even with python server pages)


I do not Have A.D.D. im just easily, Hey look a Squirrel!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top