$_POST Giving Empty values [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have an HTML Form and I am trying to get the values from the form in my php file.
<div id="qForm">
<form action="postComment.php" method="post" name="comment">
Name: <input type="text" name="askerName"><br>
Title: <input type="text" name="qTitle"><br>
<textarea maxlength="255" name="theQ"></textarea>
<input type="submit">
</form>
</div>
My PHP file looks like this but It echos as Welcome
<?php
$name = $_POST["askerName"];
echo $name; //this works
$textArea = $_POST["theQ"];
echo $textArea; //this does not work
?>
<html>
<head>
</head>
<body>
welcome <?php $_POST["askerName"]; ?>
</body>
</html>

Use
<body>
welcome <?php echo $_POST["askerName"]; ?>
</body>
instead.

you have to use print/echo to display your php output these two basic ways to get output: echo and print
<body>
welcome <?php print $_POST["askerName"]; ?>
</body>
else
<body>
welcome <?php echo $_POST["askerName"]; ?>
</body>
echo is marginally faster compared to print as echo does not return any value

Related

PHP session not showing a value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
<?php
session_start();
?>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="pageContainer">
<form action="second.php" class="formLayout" method="post">
<div class="formGroup">
<label>Name:</label>
<input type="text" name="first_name"><br>
<input type="hidden" name="postback" value="true"><br>
</div>
<div class="formGroup">
<label> Car model:</label>
<div class="formElements">
<input type="radio" name="model" value="Mustang">Ford Mustang<br>
<input type="radio" name="model" value="Subaru">Subaru WRX STI<br>
<input type="radio" name="model" value="Corvette">Corvette<br>
</div>
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$_SESSION["first_name"] = $_POST["first_name"];
$_SESSION["model"] = $_POST["model"];
}
?>
</div>
</body>
</html>
I set up my code to set the value of the "first_name" and "model" names into my session variables.
When I try to access the values of the stored variables in the submission page of the form:
<?php
session_start();
?>
<html>
<body>
<?php
echo $_SESSION["first_name"];
echo $_SESSION["model"];
?>
</body>
</html>
I only receive the out of the model value, not the first_name value. I don't understand what I'm doing wrong here.
Let's assume your first file is called first.php. As shown in your <form>, the second one is second.php.
It seems that you are writing the data into the session in the first.php file, but this code will never run because you are submitting your form to second.php and not first.php!
So, move the code that writes the session variables into second.php instead. To test that it really worked, you can create a third.php to display them.
(I'm not sure why see the model set, but I guess it's still there from a previous test you did.)

How to send data from button click to php in the same file without using ajax [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
<html>
<body>
<?php
echo "Hello Wolrd!";
if(isset($_GET['submit'])) {
echo "hello";
}
else {
}
?>
<h2> Sending data onclick </h2>
<button name = "submit" id="submit" onclick="<?php echo $_SERVER['PHP_SELF']; ?>" > order </button>
</body>
</html>
This is the code how I am trying to work. On clicking the button the php script should be executed but nothing is happening.
You need to create a form with action blank or same page name and use GET or POST or REQUEST in form method. This will help you to send the data on the same page and you can use some PHP code to display the data in whatever format you want.
Try this:
<html>
<body>
<?php
echo "Hello World!"; // will print when we run the program
if(isset($_GET['submit']))
{
echo "Hello World"; // will print this after form submission.
}
?>
<h2> Sending data onclick </h2>
<form action="" method="GET">
<input type="submit" value="Order" name="submit">
</form>
</body>
</html>
<form action="" method="post">
<input type="text" name="inputField">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
echo "$_POST[inputField]";
}
else {
// Code here
}
?>
PHP is a server side language and the button on click event is on the client side. In your code the php code given inside the on click gets executed at the server side when the user requests the page and the output will be placed there.
The best way for doing what you need. Like submitting data to the same PHP page when the user clicks the button is to create a in html.
<?php
if(isset($_POST['submit']))
{
echo "Data";
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="submit" name="submit" value="SomeValue" >
</form>
Or the other way is use PHP Session variables
<?php
// BEST WAY TO PASS DATA BETWEEN PHP PAGES
session_start();
if(isset($_SESSION['YOUR_SESSION']))
{
echo "Session Data";
}
?>

How to control the execution of PHP code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
with this example I recap my problem:
The first time I execute this php page I want to echo "I have two buttons".When the user click buttons "next" or "next1" I want to echo "I already said I have two buttons" and not "I have to buttons" again.I proved also with hidden input text but it doesn't work. thk for help
<?php
if ($_SESSION['already_said'] == false ){
echo "I have two buttons". date('h:i:s')."<br>";
}
$_SESSION['already_said']=true;
if( isset($_GET["next"]) || isset($_GET["next1"])) {
echo "I already said I have two buttons". date('h:i:s')."<br>";
}
?>
<html>
<body>
<form name="form" method="GET" action="" >
<button type="submit" name="next">
<span class="label">Next</span>
</button>
<button type="submit" name="next1">
<span class="label">Next1</span>
</button>
</form>
</body>
</html>
Of course the important thing is to remember to put the session_start() at the top of any script that uses the session. In fact it is a good idea to put it in all your scripts even if a few dont use the session just to keep the session alive.
<?php
session_start();
$msg = "I have two buttons ". date('h:i:s')."<br>";
if (! isset($_SESSION['already_said']) ){
$_SESSION['already_said'] = true;
}
if( (isset($_GET["next"]) || isset($_GET["next1"]) ) && isset($_SESSION['already_said'])) {
$msg = "I already said " . $msg;
}
?>
<html>
<body>
<div> <?php echo $msg; ?></div>
<form name="form" method="GET" action="" >
<button type="submit" name="next">
<span class="label">Next</span>
</button>
<button type="submit" name="next1">
<span class="label">Next1</span>
</button>
</form>
</body>
</html>

php Session not working in chrome and IE [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have php session not working in chrome and IE but working fine in Firefox.
I'm getting this error in page4.:
it show variable empty in page4, session not passing in page4 after the user click click me in page3
Notice: Undefined index: username in /var/www/html/phptest/test4.php on line 5
Please see my code and let me know where the error is.
Page2
<html>
<body>
<form action="test3.php" method="post">
Username: <br><input type="text" name="username"></br>
<input type="submit" name = 'submit1' value= 'Login'>
</form>
</body>
</html>
Page3
<html>
<body>
<?php
session_start();
$username = $_POST['username'];
$_SESSION['username']= $_POST['username'];
echo "<br> Hi $username.</br>";
?>
<form action="test4.php" method="post">
<input type="submit" name = 'submit' value= 'click me'>
</form>
</body>
</html>
Page 4
<?php
session_start();
$username = $_SESSION['username'];
echo "<br> Hi $username.</br>";
?>
session_start() must go at the top of the page before any output:
<?php
session_start();
?>
<html>
<body>
<?php
$username = $_POST['username'];
$_SESSION['username']= $_POST['username'];
echo "<br> Hi $username.</br>";
?>

PHP not showing up values for Post function

Hye there I'm new to PHP and learning to my own I have a simple HTML form as:
<form action="file:///C|/wamp/www/welcome.php" method="POST">
Enter your name: <input type="text" name="name" />
Enter your age: <input type="text" name="age" />
<input type="submit" />
and my PHP file is:
<body>
HELLO
<?php
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old!
?>
</body>
the problem is that when i click on my submit button it only shows the following output:
HELLO Welcome .
You are years old!
I mean the output is not showing up the contents from the POST Function! so is it me doing something wrong or so. I am new to PHP and want to learn it can somebody help me please
thanks in advance!
The path for your form action must be a valid web path. Not a file path on your computer:
<form action="/welcome.php" method="POST">
or
<form action="http://localhost/welcome.php" method="POST">
You also mix HTML in your PHP which should be throwing you a syntax error. When you fix the above that will error out on you.
<body>
HELLO
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old!
</body>

Categories