-2

I want to validate if the textbox has a value or not. Right now what I have is a textbox that has a value but the output says it is empty here is it it is like nothing is being conditioned on the code please see me code, thank you

Full Code

-Here is the full code of my form please take a look thank you very much

    <form>
          <div class="row">
            <form method="POST">
            <div class="col-md-8">
              <?php
              $code = 'Code';
              $code2 = 'PIN'; 
            if(isset($_POST['btnSubcode'])) {
              $lblCode  =  isset($_POST['lblQrTxt']) ? $_POST['lblQrTxt'] : '';

              $code =   $lblCode;
              $code = explode(":",$code); // code = array("QR Code","444444444|123")
              $code = explode("|",$code[1]); // code[1] = "444444444|123"
              $code = trim($code[0]); // 444444444


              $code2 =   $lblCode;
              $code2 = explode(":",$code2); // code = array("QR Code","444444444|123")
              $code2 = explode("|",$code2[1]); // code[1] = "444444444|123"
              $code2 = trim($code2[1]); // 123
            }


            ?>


              <div class="form-group">
                <label class="form-control-label">code</label>
                <input type="text" name="input" id="card-code" value='<?php echo $code ?>' class="form-control">
              </div>
            </div>
            <div class="col-md-4">
              <div class="form-group">
                <label class="form-control-label">pin</label>
                <input type="text" id="card-pin" value='<?php echo $code2 ?>' class="form-control" maxlength="3">
              </div>

             <?php 


              if(isset($_POST['txtQrtxt']) && $_POST['txtQrtxt'] != '') {
                 echo "Text Present";
              } else {
                 echo "Text Not Present";
              }

              ?>

                        <div class="caption">

                        <div class="jumbotron">
                        <input type="text" name='txtQrtxt' value='Hello World' class="form-control" >

                        <textarea class="form-control text-center" id="scanned-QR" name="lblQrTxt"></textarea><br><br><br>

                         </div>

                        </div>


              </form>
              <div class="form-group float-right">
                <input value="Topup" class="btn btn-primary topup-button">

              </div>
            </div>
          </div>

        </form>


 <?php 

              $txtCodeqr  =  isset($_POST['txtQrtxt']) ? $_POST['txtQrtxt'] : '';
              if (!empty($txtCodeqr)) {
                echo "Text";
              } else {
                echo "Empty Textbox";
              }


               ?>

my textbox

<input type="text" name='txtQrtxt' value='Hello World' class="form-control" >
  • How is the value being sent to the server? Please provide a more complete example. – David Jan 28 '19 at 02:18
  • it has no database sir\ –  Jan 28 '19 at 02:20
  • @David , it is send by just inputing the value on the textbox im just checking it if it has a value or not. –  Jan 28 '19 at 02:21
  • You are *assuming* the value is being sent to the server. PHP is telling you otherwise, and it's usually right about these things. Don't *assume* the rest of your code is working, as that could be where the problem is. Provide a *complete* example of the problem so we can help you. Otherwise all we can do is assume you don't have any code which is sending the value to the server. – David Jan 28 '19 at 02:22
  • I am quite new also to php. But why your form dont have "action"?
    it has method POST, but to which php file you are posting this form to?
    – GeneCode Jan 28 '19 at 02:37
  • @GeneCode it doesnt have sir because i dont need to use the submit button –  Jan 28 '19 at 02:38
  • I mean normally form is like this to post:
    – GeneCode Jan 28 '19 at 02:39
  • if there is no submitting, then how can you get $_POST variable? I think something is not right. But then again i maybe wrong cause i am newbie. – GeneCode Jan 28 '19 at 02:43
  • 1
    @obitouchiha: *"i dont need to use the submit button"* - Why do you think that? You've been asked several times how you are submitting the value to the server, and you're now indicating that you aren't. Which *really explains* why the value isn't being submitted to the server. Your form is incomplete and you *never submit it*. It's time to go back to the PHP tutorials and learn about forms. – David Jan 28 '19 at 02:48
  • @GeneCode yes sir, but in my case i dont need to use submit button, becuase what im doing is a qr code reader o when the image is red , then it has to submit to a textbox that is why i need to validate if it is passed or not –  Jan 28 '19 at 03:02
  • Are u scanning qr code in a phone or something? – GeneCode Jan 28 '19 at 03:07
  • @GeneCode no sir it's on the web , idk why this question is getting down voted :( all i want is to validate the textbox if it has a value that is why i dont need a submit button –  Jan 28 '19 at 03:12
  • @obitouchiha: If you want to validate the text in the browser and not on the server then you need to validate it in JavaScript and not in PHP. This would be worth reading and understanding: https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – David Jan 28 '19 at 03:14
  • How are you getting the qrcode into the textbox? Is it automatically filled in by another program? I am having problem to understand the flow of your application. If the textbox is being filled automatically by external process, then you still need to trigger the checking, which you cannot do without interaction from user (click submit or some other way). – GeneCode Jan 28 '19 at 06:58

2 Answers2

1

You might be over complicating it. It is pretty simple.

<?php

if(isset($_POST['txt']) && $_POST['txt'] != '') {
   echo "Text Present";
} else {
   echo "Text Not Present";
}
?>

Additionally I would recommend you filter all input on post or get. Basically anything that gets information from a user.

Check here - http://php.net/manual/en/function.filter-input.php

<?php

$my_txt = filter_input(INPUT_POST, 'txt');
if(isset($my_txt) && $my_txt != '') {
   echo "Text Present";
} else {
   echo "Text Not Present";
}
?>

Also you need to add a submit button between your form tags. Like this.

<input type="submit" value="Submit">

Also you should have only one closing tag for every opening tag. This is called valid HTML.

For example a valid form is like

 <form method="post">
  First name:<br>
  <input type="text" name="firstname" value="Mickey"><br>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse"><br><br>
  <input type="submit" value="Submit">
</form> 
Goddard
  • 2,863
  • 31
  • 37
  • sir i tried it when it says **Text Not Present** , tho i have the value of `Hello World` and even i delete the value of the textbox `Text Not Present` still presists :( why –  Jan 28 '19 at 02:27
  • elaborate on your question by showing your form. That will help. – Goddard Jan 28 '19 at 02:27
  • hello sir i've edited my question i added the full code of the form and inputs above please take a look thank you very much ! –  Jan 28 '19 at 02:31
  • @obitouchiha: Where's the "submit" button for your form? How are you submitting it? – David Jan 28 '19 at 02:32
  • You don't have a submit button and you have two tags. – Goddard Jan 28 '19 at 02:33
  • @Goddard , Can you please add a snippet sir ? please ? that will be very helpful to us thank you again sir. –  Jan 28 '19 at 02:33
1

Ok I have made a simple php test file and tested it works. Your problem is:

  1. You don't have a submit button. The $_POST will not be there if you do not submit a form first.

  2. It would be easier to validate your textarea using javascript instead.

Here is my test file and it works:

<html>
<body>
<form method="POST">
<textarea name="txtQrtxt">
</textarea>

<input type="submit">
</form>

<?php

$var = $_POST['txtQrtxt'];

if (strlen($var)<=0) {
    echo "Textarea empty";
} else {
    echo "Textarea Okay";   

}


?>
</body></html>
GeneCode
  • 7,545
  • 8
  • 50
  • 85