0

I'm generating an HTML form with the help of another form, when i check some boxes and press on submit button, the page just refresh without any result,

My code list some orders, so we can choose them with the check boxes, then i could receive values of checkedOrder[], and do other things.

Here a bit of code

<form class="form_f" method="post">
<div class="orders"><?php
while ($i !=  count($id_order_F))
{
    ?>
    <label for="checkedOrder<?php echo $i; ?>">
        <div class="order">
                <input id="checkedOrder<?php echo $i; ?>" type="checkbox" name="checkedOrder[]" value="<?php echo $id_order_F[$i]; ?>">
                <span>Commande N°<?php echo $id_order_F[$i]; ?></span><br>
                <span>Référence : <?php echo $reference_F[$i]; ?></span><br>
                <span>Statut : <?php echo $order_state_name_F[$i]; ?></span><br>
                <span>ID Adresse livraison : <?php echo $id_address_delivery_F[$i]; ?></span><br>
                <span><?php echo number_format($total_paid_tax_excl_F[$i], 2, ',', ''); ?>€ HT</span><br>
                <span><?php echo number_format($total_paid_tax_incl_F[$i], 2, ',', ''); ?>€ TTC</span><br>
        </div>
    </label>
    <?php
    $i++;
}

?> </div>  
        <div class="btn_f">
            <input class="input-b" type="submit" name="fusion" value="Fusionner les commandes">
        </div>
</form>
<?php

// nothing display !
    if (isset($_POST['fusion'])) {
        echo "fusion set";
        $order_checked = $_POST['checkedOrder'];
        foreach ($order_checked as $order_display)
        {
            echo $order_display . "<br />";
        }
    }

HTML generated look like that :

<form class="form_f" method="post" action="/script.php">
<div class="orders">
    <label for="checkedOrder0">
        <div class="order">
                <input id="checkedOrder0" type="checkbox" name="checkedOrder[]" value="5818">
                <span>Commande N°5818</span><br>
                <span>Référence : SRFYGXCTD</span><br>
                <span>Statut : Commande acceptée</span><br>
                <span>ID Adresse livraison : 17434</span><br>
                <span>1016,69€ HT</span><br>
                <span>1220,02€ TTC</span><br>
        </div>
    </label>
    <label for="checkedOrder1">
        <div class="order">
                <input id="checkedOrder1" type="checkbox" name="checkedOrder[]" value="5821">
                <span>Commande N°5821</span><br>
                <span>Référence : GOQBCCPSL</span><br>
                <span>Statut : En cours de traitement</span><br>
                <span>ID Adresse livraison : 17434</span><br>
                <span>1360,80€ HT</span><br>
                <span>1632,96€ TTC</span><br>
        </div>
    </label>
    <label for="checkedOrder2">
        <div class="order">
                <input id="checkedOrder2" type="checkbox" name="checkedOrder[]" value="5857">
                <span>Commande N°5857</span><br>
                <span>Référence : EQALZUQNH</span><br>
                <span>Statut : En cours de traitement</span><br>
                <span>ID Adresse livraison : 17434</span><br>
                <span>44,68€ HT</span><br>
                <span>53,62€ TTC</span><br>
        </div>
    </label>
    <label for="checkedOrder3">
        <div class="order">
                <input id="checkedOrder3" type="checkbox" name="checkedOrder[]" value="5858">
                <span>Commande N°5858</span><br>
                <span>Référence : KMQESFABG</span><br>
                <span>Statut : En cours de traitement</span><br>
                <span>ID Adresse livraison : 17434</span><br>
                <span>607,71€ HT</span><br>
                <span>729,25€ TTC</span><br>
        </div>
    </label>
    <label for="checkedOrder4">
        <div class="order">
                <input id="checkedOrder4" type="checkbox" name="checkedOrder[]" value="5819">
                <span>Commande N°5819</span><br>
                <span>Référence : NFHSUXBZG</span><br>
                <span>Statut : En attente d'autorisation</span><br>
                <span>ID Adresse livraison : 17434</span><br>
                <span>250,00€ HT</span><br>
                <span>300,00€ TTC</span><br>
        </div>
    </label></div>  
        <div class="btn_f">
            <input class="input-b" type="submit" name="fusion" value="Fusionner les commandes">
        </div>
</form>

edit : Does a php script can handle 2 forms ? Here the user type an id, then press submit, a form is created with a list of orders, the user check all orders he want and then press submit. It seems like after submitting the first time, my script don't detect anything else

6
  • 1
    You need to fill in the action attribute in your html form opening tag. $_SERVER['PHP_SELF'] is the most common. html.form.guide/php-form/php-form-action-self Commented Jun 2, 2022 at 15:42
  • And how does yout HTML looks like after the form is dynamicly created by PHP Commented Jun 2, 2022 at 15:42
  • I have edited the post so you can see the html generated, i tried to put $_SERVER['PHP_SELF'] in the action attribute but no effect ! Commented Jun 2, 2022 at 15:50
  • What IDE do you use? Commented Jun 2, 2022 at 15:55
  • Rather than using $_SERVER['PHP_SELF'] as the form action ( it is vulnerable to abuse btw ) omit the action entirely. Commented Jun 2, 2022 at 15:55

1 Answer 1

1

I copied your HTML and your PHP post part into PHPstorm to reproduce your problem. I noticed you have a typo in your post part. After I changed that the post request worked just fine

if (isset($_POST['fusion']))
    echo "bouton fusion ok";
var_dump($_POST['checkedOrder']);
$order_checked = $_POST['checkedOrder'];
foreach ($order_checked as $order_display){
    echo $order_display . "<br />";
}

Should be:

    if (isset($_POST['fusion'])) { // This bracket was missing
      echo "bouton fusion ok";
      var_dump($_POST['checkedOrder']);
      $order_checked = $_POST['checkedOrder'];
      foreach ($order_checked as $order_display) {
        echo $order_display . "<br />";
      }
    } // This bracket was missing

If you write PHP and HTML into the same file I would suggest putting your logic code in top and your html code at the end. I should also write the HTML different for a better overview. Give a sec and I will post on how I should do it

Edit: Below the code how you could also write your PHP in combinatio with HTML in 1 file. I prefer to close the PHP tag at the end of my logic so I can type normal HTML. When I need a PHP function or variable You can open PHP again. To just echo out a variable you can simply use <?= $var;?> The = is in this case a shorthand to echo.

For functions I like if else I like to use collins instead of breackets for better readability so instead of:

<?php if($var) { ?>
// html or other code here
<?php } ?>

You can do:

<?php if($var): ?>
//html or other code here
// When you have alot of code here this will make the reading a bit easier
<?php endif; ?>

To be clear this is not how you should write your code but maybe it can help you to improve the readability of your code when you use PHP and HTML in 1 file. If it gets to complex I would suggest going to use a MVC structure or framework.

<?php
// Most of your logic
if (isset($_POST['fusion'])) {
  echo "bouton fusion ok";
  var_dump($_POST['checkedOrder']);
  // Make sure the data is posted and if its an array else create an empty array
  $order_checked = isset($_POST['checkedOrder']) && is_array($_POST['checkedOrder']) ? $_POST['checkedOrder'] : [];
  foreach ($order_checked as $order_display) {
    echo $order_display . "<br />";
  }
}
// Most of your HTML
?>

<form class="form_f" method="post" action="">
  <div class="orders">

    <?php while ($i != count($id_order_F)): ?>

      <label for="checkedOrder<?= $i;?>">
        <div class="order">
          <input id="checkedOrder<?= $i;?>" type="checkbox" name="checkedOrder[]" value="<?= $id_order_F[$i]; ?>">
          <span>Commande N°<?= $id_order_F[$i]; ?></span><br>
          <span>Référence : <?= $reference_F[$i]; ?></span><br>
          <span>Statut : <?= $order_state_name_F[$i]; ?></span><br>
          <span>ID Adresse livraison : <?= $id_address_delivery_F[$i]; ?></span><br>
          <span><?= number_format($total_paid_tax_excl_F[$i], 2, ',', ''); ?>€ HT</span><br>
          <span><?= number_format($total_paid_tax_incl_F[$i], 2, ',', ''); ?>€ TTC</span><br>
        </div>
      </label>

    <?php endwhile; ?>

  </div>
  <div class="btn_f">
    <input class="input-b" type="submit" name="fusion" value="Fusionner les commandes">
  </div>
</form>
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your help, i put the brackets, without any result. i will try your method but if it's just about readability it will probably not work too. I'm coming back afters some tests :)
So i changed my code, but i have the same issue. "if (isset($_POST['fusion'])) { echo "fusion set"; " display nothing like it wasn't set at all
@Quentin Then there is something else wrong in your project you didnt post here. I copied your code, added the brackets and it worked
Does a php script can handle 2 forms ? Here the user type an id, then press submit, a form is created with a list of orders, the user check all orders he want and then press submit. It seems like after submitting the first time, my script don't detect anything else
A PHP script can handle as many forms you whant you can easly take care of them in PHP aslong the name in the submit button is different, but make sure you dont output a html form into an other html form.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.