1

I want make some php test in my form and i've followed a tutorial but i don't get the result i wanted. I want to make sure that the borne's value isn't null. Then send the value to another page "exo.php" to use it.

The problem is when i add the php code inside input's value for example it's not cosindring it as php code but as a string so it prints the php code.

this is my code :

<?php
    if (isset($_GET["submit"])) {
      $borneErr="";
      $borne="";
      if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["borne"])) {
          $borneErr = "Missing";
        }
        else {
          $borne = $_POST["borne"];
        }
      }
    }
    ?>
    <form class="" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
      <label> Borne : <input type="text" name="borne" value="<?php echo htmlspecialchars($borne);?>">
      <span class="error">* <?php echo $borneErr;?></span>  </label>
      <button type="submit" value="OK"> Send !</button>
    </form>

this is the result i get in this image below :

enter image description here

Abdo Rabah
  • 1,670
  • 2
  • 15
  • 31

3 Answers3

2

just add value inside quotation mark "HERE"

<input type="text" name="borne" value="<?php echo htmlspecialchars($borne);?>">
ham999dy
  • 36
  • 3
2

Your code raises notice

Undefined variable: borne

on this line:

<label> Borne : <input type="text" name="borne" value="<?php echo htmlspecialchars($borne);?>">

And also this notice

Undefined variable: borneErr

on this line:

<span class="error">* <?php echo $borneErr;?></span>  </label>

You can fix that by defining the variable outside of the condition.


The form has a method="POST" attribute. But you're checking the condition against GET data:

if (isset($_GET["submit"])) {

Also, you're checking existence of a field submit that is not included in the form data, since it's a <button>. You can either change it to <input> or change your PHP condition to check the borne field.

<input type="submit" value="Send !">

or

if (isset($_POST["borne"])) {

The check against $_SERVER["REQUEST_METHOD"] is now redundant so you can get rid of it.


The code could be simplified and polished even more but I'll leave it so it's easier see those errors fixed.

Working code:

<?php
$borneErr = "";
$borne = "";
if (isset($_POST["borne"])) {
    if (empty($_POST['borne'])) {
        $borneErr = "Missing";
    } else {
        $borne = htmlspecialchars($_POST['borne']);
    }
}
?>
<form class="" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
  <label> Borne : <input type="text" name="borne" value="<?php echo $borne; ?>">
  <span class="error">* <?php echo $borneErr;?></span>  </label>
  <input type="submit" value="Send !">
</form>
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • Thank you for your help..I have a question is how to to use the value of borne in another page? do i need to change in form's action ? – Abdo Rabah Feb 09 '19 at 02:08
  • @AbdoRabah Yes, changing the action is one of the ways. You can also store the value to database on first page and read it from the DB on the second page. Or use sessions. – Petr Hejda Feb 09 '19 at 09:56
0

You can simply choose one here

<input type="text" name="borne" value="<?=$_SERVER["PHP_SELF"]?>"><Br>

OR

<input type="text" name="borne" value=""> /* leave it blank cause if blank i will submit automatically on the same page. */
xenon
  • 90
  • 4