Strange if structure

the idea was to let users choose file/folder by typing into input.
here is the script:
<?php
$w=‘’;
if(isset($_POST[‘word’])and $_POST[‘word’]==‘theword’)
$w= ‘w1’;
else $w= ‘w2’;
?>

<form action=“<?php echo $w;?>” method=“post”>
<input type=“text” name=“word” id=“word” maxlength=“5”>
<br>
<input type=“submit” value=“Send!”>
</form>

All the script results in is opening w2.
What’s wrong with my php logic?

How can you enter ‘theword’ (7 chars) when the textbox’s maxlength = 5?

ok, you’re quite right.
I tried to make the script look nicer before presenting it to the forum, in the original it was :
<html><head></head><body>
<?php
$w=‘’;
if(isset($_POST[‘word’])and $_POST[‘word’]!==‘q’)
$w= ‘w1’;
else $w= ‘w2’;
?>
<form action=“<?php echo $w;?>” method=“post”>
<input type=“text” name=“word” id=“word” maxlength=“5”>
<br>
<input type=“submit” value=“Send!”>
</form>

</body></html
but it didn’t work as expected and only kept opening w2.

Use curly brackets correctly to define the code blocks.


<?php
$w='';

if(isset($_POST['word']) && $_POST['word']!=='q'){

$w= 'w1';

} else { 

$w= 'w2';

}
?>

BTW, depending on your situation you could simplify it like this:


<?php
$w= 'w1'; // this is the default, so set it here

if(isset($_POST['word']) && $_POST['word']!=='q'){
$w= 'w2' ;
}

// then you could drop the curly brackets if you wanted to
// if(isset($_POST['word']) && $_POST['word']!=='q')
//         $w= 'w2' ; // if I drop the brackets, I indent then leave a clear line

// rest of your code
?>

Be aware though that $w will be equal to w1 even if $_POST[‘word’] is not set, but is that what you want?

*Edited to show && instead of and, I find that easier to read - much is down to personal preference :wink:

So what’s the problem then?

You haven’t said what the name of the file your posted code is in so I am assuming it’s not w1 or w2. Then when you open the page your posted code is in, the form’s action will be set to w2. Then when you click the Send button. the value in the textbox will be sent to w2. You haven’t shown the code in w1 or w2 so who knows what’s in either of them.

w1 is a folder containing a ‘hi,world!’ index.php,
w2 is a folder with a ‘hi, world2!’ index.php.
the matter is that php code in form action attribute does not open w1 when input ‘word’ has ‘q’ typed in,
moreover the script gets a notice about index ‘word’ not being defined.
is input’s name not enough definition?

Welcome to the SP forums.

Could you please post the real code, not some ‘made nicer for the forum’ version? I’m sure you’re doing it to post only the relevant code, but it seems to me we’re missing something.
And could you post the exact error you’re getting?

well, here is the unshaven script:

<html><head></head><body>
<?php
$w= ‘’;
$explanation=print “Type in ‘x’, ‘y’ or ‘z’.”;

if(isset($_POST[‘word’])and $_POST[‘word’]==‘x’)
{
$w= ‘w1’;
}
elseif(isset($_POST[‘word’])and $_POST[‘word’]==‘y’)
{
$w=‘w2’;
}
elseif(isset($_POST[‘word’])and $_POST[‘word’]==‘z’)
{
$w= ‘w3’;
}
else {
$w=‘’;

}

php print $explanation;?>

<form action=“<?php echo $w;?>” method=“post”>
<input type=“text” name=“word” id=“word” maxlength=“5”>
<br>
<input type=“submit” value=“Send!”>
</form>
</body>
</html>

It looks logical and it works but only once.
If after getting to w1 (or w2, or w3) user realizes s/he’d rather get to another ‘w’ and hits the ‘back’ button then no matter what is typed into input (or left blank) the script inevitably lands them onto the page of their first choice.
The only way out is to copy-paste the form’s url into browser’s url address line and hit ‘enter’ but you have no moral right to expect it from user.
Another queerness of the script is unsolicited number sticking in the form’s page and it looks as follows:

Type in ‘x’, ‘y’ or ‘z’.1


Send!


That ‘1’ is caused by the following:


$explanation=print "Type in 'x', 'y' or 'z'.";   [COLOR="#FF0000"][B]<-- print displays the text, and then returns 1 (see the manual http://www.php.net/print), and that value 1 is assigned to $explanation[/B][/COLOR]
...
print $explanation;  [COLOR="#FF0000"][B]<-- displays the value of $explanation, i.e. 1[/B][/COLOR]

The problem you’re having with the several ‘w’ values is a little more difficult to explain.

  1. The first time you call the script, $w = ‘’, so the form’s action is empty (and it should call the script itself)
  2. When the user enters for example ‘y’ and sends the form, $w = ‘w2’, and the same form is displayed again, but with action ‘w2’
  3. When the user sends the form for the second time, script ‘w2’ is called

At this point, if the user clicks ‘back’, the browser will return to 2), which is the form with action ‘w2’. The user would have to press ‘back’ another time to return to 1).

The question is: why do you want the user to send the form twice to end up on page ‘w2’? Why don’t you redirect immediately to ‘w2’?

the intention was to allow user make their choice with just one form so the script is a try to implement it.
You are right – two-step redirection is not elegant.
Is having 3 forms on a page (one for each ‘w’) nicer?
That is the question.
P.S. thank you for illucidating on ‘print’ side-effect.