2

When I wanted to test whether the value is properly passed from HTML form to PHP, I found something is wrong with the echo statement. Here is the HTML text box code

<div class = form-group> <label for = 'bcaa'>BCAA 5gms Cost</label> <input type = 'text' class = 'form-control' name = 'bcaa'> </div> 

And here is the PHP code to get the value and print it simply.

<?php $bcaacost = isset($_POST['bcaa']) ; echo $bcaacost; ?> 

The value given in the text box is not printed, but simply '1' is printed the more echo statement is added more no of '1' is printed

Like

echo "something"; echo "blah blah"; 

The output is

11 

What's the reason for this and what do I need to do to fix this?

2
  • 2
    The result of an isset is a boolean TRUE or FALSE. That should be all you need to know Commented May 10, 2016 at 11:25
  • 1
    $bcaacost = isset($_POST['bcaa']) ? $_POST['bcaa'] : ""; Commented May 10, 2016 at 11:27

5 Answers 5

2

Remove isset() for variable assigning as it checks veriable $_POST['bcaa'] is set or not.

$bcaacost = $_POST['bcaa']; 

Full code

<?php $bcaacost = $_POST['bcaa']; echo $bcaacost; ?> 

Read more about isset()

Sign up to request clarification or add additional context in comments.

Comments

1

isset() returns true or false depending on the variable set or not.

$bcaacost = isset($_POST['bcaa']) ; // return true / 1 echo $bcaacost; // prints 1 

it should be like -

if(isset($_POST['bcaa'])) { echo $_POST['bcaa']; } 

Or if you want to print some default, then -

$bcaacost = isset($_POST['bcaa']) ? $_POST['bcaa'] : 'Default'; echo $bcaacost; 

Comments

1
if(isset($_POST['bcaa'])) { $bcaacost = $_POST['bcaa'] ; echo $bcaacost ; } 

Otherwise, isset($_POST['bcaa']) will print 1 or 0

Comments

1

isset() checks that a variable exists, and returns either true(1) or false(0)

To see the contents of $_POST['bcaa'] you need to check it exists i.e. isset() but then use the contents of the variable

<?php $bcaacost = isset($_POST['bcaa']); if ( $bcaacost ) { echo $_POST['bcaa']; } ?> 

Or more simply

if ( isset($_POST['bcaa']) ) { echo $_POST['bcaa']; } 

As to your suggestion that :

echo "something"; echo "blah blah"; 

will create 11 that cannot be so. Something else must be causing this or you are just confused as to what is creating the 11 output

Comments

0

the result of isset is true or false. In your case the result is true and it assigned back to $bcaacost and that's why it echoing 1.

use following code:

<?php $bcaacost = isset($_POST['bcaa'])?$_POST['bcaa']:"not set"; echo $bcaacost; ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.