1

I have a recent issue with a $_POST.

When I write :

print_r($_POST['nb_acces']);

I get :

1

Which is the value I wrote in the form.

But if I write :

$nb_acces = $_POST['nb_acces'];
print_r($nb_acces )

I get :

Array ( [nb] => 0 )

It was working before, and now I don't understand what happens. Could it be from a server configuration or something ?

EDIT :

My input :

<form id='sous_paiement' name='sous_paiement' method='post' action='commande_enregistre_test.php?ecole=<?php echo $id_ecole; ?>'>
    <input id='nb_acces' name='nb_acces' style='width:90px;border:1px solid black;padding-left:4px;text-align:center;' />

Yes if I use $_POST['nb_acces'] I can echo the value. Though I wish to check the value to have an integer and avoid injection.

The way I get this array is the point I don't understand. I have only one input.

Could it come from my checking JavaScript function (sorry for french words, I remove here any non digit characters and calculate some prices) :

function filtre_saisie() {
      var input_acces = document.getElementById("nb_acces");
      var nb_acces = input_acces.value.replace(/[^0-9]/g, '');
      input_acces.value = nb_acces;

      var prix_unitaire = calcul_prix_unitaire(nb_acces);
      var prix_total = calcul_prix_total(nb_acces);

      document.getElementById("div_prix_unitaire").innerHTML = prix_unitaire;
      document.getElementById("div_prix_total").innerHTML = prix_total;
    }

Could it be this replace ?

EDIT 2 ----------------------------------

Well the whole form :

<form id='sous_paiement' name='sous_paiement' method='post' action='commande_enregistre_test.php?ecole=<?php echo $id_ecole; ?>'>
      <div class="div_categories">
        <p style='margin:0;padding:3px;padding-left:60px;'>Indiquez le <strong>nombre d'accès</strong> que vous souhaitez acheter :</p>
      </div>

      <br />
      <div id="erreur_nb_acces" style="color:red;"><br /></div>


      <table style='border-collapse:collapse;'>
        <tr style='font-weight:bold;border:1px solid #eee;background-color:white;'>
          <td style='width:200px;padding-left:10px;'>Nombre d'accès</td>
          <td style='width:100px;padding-left:10px;padding-right:10px;'><input id='nb_acces' name='nb_acces' type="text" style='width:90px;border:1px solid black;padding-left:4px;text-align:center;' /></td>
        </tr>
      </table>

      <br />

      <table style='border-collapse:collapse;'>
        <tr style='border:1px solid #eee;background-color:white;border-bottom:1px solid #999;'>
          <td style='width:200px;padding-left:10px;'>Prix par accès</td>
          <td style='width:100px;padding-left:10px;padding-right:10px;text-align:right;'><div id='div_prix_unitaire' style='display:inline;'>-</div> &euro;</td>
        </tr>
        <tr style='font-weight:bold;border:1px solid #999;background-color:white;'>
          <td style='width:200px;padding-left:10px;'>PRIX TOTAL</td>
          <td style='width:100px;padding-left:10px;padding-right:10px;text-align:right;'><div id='div_prix_total' style='display:inline;'>-</div> &euro;</td>
        </tr>
      </table>

      <div style='width:190px;margin-top:5px;'>
        <a href='#' onclick='ouvrirPopUp_tarifs();'><img src='../images/IconeInformation.png' style='border:0px solid red;float:left;display:inline;' /><span style="line-height:24px;">Consulter les tarifs</span></a>
      </div>
      <div style="clear:both;"></div>

      <br /><br />
      <div class="div_categories">
        <p style='margin:0;padding:3px;padding-left:60px;'>Choisissez votre <strong>mode de paiement</strong> :</p>
      </div>

      <br /><br />

      <table class="tab_mode_paiement" style="border-collapse:collapse;">
        <tr>
          <td>
            <input id='paiement_cb' name='mode_paiement' value='5' class='eleves' type='radio' checked="checked" style="cursor:pointer" />
          </td>
          <td style="font-weight:bold;padding-right:80px;border-right:1px solid #999;">
            <label for='paiement_cb' style="cursor:pointer">
              <img src="../images/ic_cb.jpg" style="float:left;display:inline;padding-right:10px;" /> Carte bancaire
            </label>
          </td>
          <td style="padding-left:40px;">
            <!--<input id='paiement_paypal' name='mode_paiement' value='3' class='eleves' type='radio' style="cursor:pointer" />-->
          </td>
          <td style="font-family:arial;font-size:20px;padding-right:120px;">
            <!--<label for='paiement_paypal' style="cursor:pointer">
              <span style="color:rgba(0,3,128,1)">Pay</span><span style="color:rgba(0,158,210,1)">Pal</span>
            </label>-->
          </td>
          <td>
            <input id='paiement_virement' name='mode_paiement' value='1' class='eleves' type='radio' style="cursor:pointer" />
          </td>
          <td style="font-weight:bold;">
            <label for='paiement_virement' style="cursor:pointer">
              Par virement bancaire
            </label>
          </td>
        </tr>
        <tr>
          <td colspan="2" style="border-right:1px solid #999;">Recevez vos accès immédiatement</td>
          <td colspan="4" style="padding-left:90px;">Accès disponibles après réception du paiement</td>
        </tr>
      </table>

      <br /><br /><br />

      <input id='enregistrer' name='enregistrer' value='Enregistrer la commande' type='button' onclick='verif_sous_paiement();' style='width:300px;height:40px;' />
      <br /><br />
      <br />
    </form>

And my Javascript :

<script>

    function ouvrirPopUp_tarifs() {
      window.open ('tarifs.php', 'tarifs', config='height=370, width=760, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no');
    }

    var tab_responsables = new Array();
    var tab_acces_responsables = new Array();
    var nb_responsables = 0;

    function initialiser_filtre_saisie() {
      setInterval("filtre_saisie()", 200);
    }

    function filtre_saisie() {
      var input_acces = document.getElementById("nb_acces");
      var nb_acces = input_acces.value.replace(/[^0-9]/g, '');
      input_acces.value = nb_acces;

      var prix_unitaire = calcul_prix_unitaire(nb_acces);
      var prix_total = calcul_prix_total(nb_acces);

      document.getElementById("div_prix_unitaire").innerHTML = prix_unitaire;
      document.getElementById("div_prix_total").innerHTML = prix_total;
    }

    function verif_sous_paiement() {
      filtre_saisie();

      var nb_acces = document.getElementById("nb_acces").value;

      if (nb_acces == 0) {
        document.getElementById("erreur_nb_acces").innerHTML = "Veuillez indiquer un <strong>nombre d'accès</strong>.";
        document.getElementById('nb_acces').focus();
      }
      else {
        document.getElementById('sous_paiement').submit();
      }
    }
  </script>

Seems my whole code afraid everybody -.-

2
  • This is reproducible? I.e. once you switch to print_r($_POST['nb_acces']) it's working? What if you have both print_rs in your script?
    – VolkerK
    Aug 29, 2015 at 13:29
  • where are you getting the keys in your array? Can you include more code?
    – nomistic
    Aug 29, 2015 at 13:38

2 Answers 2

0

You are missing the type="text" attribute in your input, even the browser asumes that's a text, this might cause you some problems;

<input type="text" id='nb_acces' name='nb_acces' .......... />

Also, seems like you're replace anything that starts with a number, like 1 with a empty value, that's why you get a 0:

var nb_acces = input_acces.value.replace(/\d/g, '')
7
  • Yes, I just removed it to check if it can change anything, but no, it changes nothing.
    – Ellno
    Aug 29, 2015 at 14:07
  • OK I will put more code. I am not very used to this replace function. It seems to work very well on my browser though.
    – Ellno
    Aug 29, 2015 at 14:14
  • Do you want to replace what? I can help you with the regex
    – Skatox
    Aug 29, 2015 at 14:17
  • I want to remove any non digit character. As it is the number of items a customer would buy. You can see now my whole code.
    – Ellno
    Aug 29, 2015 at 14:20
  • I've updated the regex on my function. I don't see any problems with the HTML, if still don't work the problem is at the backend.
    – Skatox
    Aug 29, 2015 at 14:25
0

Well, I saw the message of someone who told me to check with var_dump, and it helped me to figure now why there is this issue. I don't know why this message has been erased as it helped me a lot. Thanks to who posted it.

If someone has the same issue, but I know it comes from an old and probably bad server configuration, to use $nb_acces and $_POST['nb_acces'] generates a conflict. Don't use the same name. That's all -.-

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.