---THIS DOES NOT WORK-----

<?php
if($string != 55)
{
echo '<form action ="<?=$_SERVER[\'PHP_SELF\']?>". method = "POST"/>';
echo '<input type="submit" name="off" value="Turn OFF Grow Light"/>';
echo '</form>';
}
?>

It display's the submit button, everything LOOK's great, but when I press the button, it goes to some screwy blank page.
However, if I'm using raw html like below, everything works fine.
---THIS WORKS---

<form action ="<?=$_SERVER['PHP_SELF']?>". method = "POST"/> 
<input type="submit" name="off" value="Turn OFF Grow Light"/> 
</form>;

As a C++ programmer and new to php , I just cannot figure out what I'm doing wrong here. I've wrapped the entire thing in single quotes, double quotes, used the escape character \' .. used brackets { } on the action variable. I know I'm missing something simple. I just can't see it. Anyone feel like pointing out where I'm going wrong? The button's show up, but when I press them it direct's to a blank page.

DOESN'T WORK

echo '<form action ="<?={$_SERVER[\'PHP_SELF\']}?>" method = "POST"/>
      <input type="submit" name="off" value="Turn OFF Grow Light"/>
      </form>';

      echo '<form action ="<?={$_SERVER[\'PHP_SELF\']}?>" method = "POST"/>
      <input type="submit" name="off" value="Turn OFF Grow Light"/>
      </form>';

I've spent an hour reading strings, I just cannot see what part here am I doing wrong?

OMG .... it was soo simple lolz.. This is why one shouldn't code whilst having 3 hour's sleep. :) Thanks allot for pointing me in the rigth direction :).

I changed the code to this and it works.
echo "<form action ='Auxiliary1.php' method = 'POST'/>";
echo "<input type='submit' name='off' value='Turn OFF Grow Light'/>";
echo "</form>";

However, I cannot get this to work. How do I pass in the $_SERVER[PHP_SELF];
THIS DOES NOT WORK
echo "<form action ='<?={$_SERVER[PHP_SELF]}?>' method = 'POST'/>";
echo "<input type='submit' name='off' value='Turn OFF Grow Light'/>";
echo "</form>";

Remove curly braces around $_server variable, its used when you want to embed variable in string.

echo "<form action ='<?=$_SERVER[PHP_SELF]?>' method = 'POST'/>";
echo "<input type='submit' name='off' value='Turn OFF Grow Light'/>";
echo "</form>";

Reverse:

  • remove the short PHP tags <?;
  • remove the shorthand = for echo as this string is already in a print construct;
  • leave the curly brackets in order to parse the variable.

So:

echo "<form action='{$_SESSION['PHP_SELF']}' method='post'>";

Better (IMHO):

$form = '<form action="%s" method="%s">';
echo sprintf($form, $_SESSION['PHP_SELF'], 'POST');

@Cody_4
See also: https://php.net/manual/en/language.basic-syntax.phpmode.php

Thanks everyone for sharing I really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.