Form submission unidentified index - php

I am trying to submit a form on the same page and I get this error:
Undefined index: text1
And:
Undefined index: text2.
Here is to code I am using:
<?php include('includes/header.php') ?>
<section class="login">
<?php
if (isset($_POST['submitbtn'])) {
$text1 = $_POST['text1'];
$text2 = $_POST['text2'];
echo "Success! You entered: " . $text1 . "<br>";
echo "Success! You entered: " . $text2 . "<br>";
} else {
?>
<form action="" method="post">
<p><label>Text1:</label><input type="text" name"text1"/></p>
<p><label>Text2:</label><input type="text" name"text2"/></p>
<input type="submit" name="submitbtn"/>
</form>
<?php } ?>
</section>
<?php include('includes/footer.php') ?>
header.php consists of an html navigation and it opens a connection to
the database.
footer.php has the footer and closes the connection.
What am I doing wrong? Also is there a better way in submitting a form on the same page?

Forget = in name"text1". It would be
<p><label>Text1:</label><input type="text" name="text1"/></p>
<p><label>Text2:</label><input type="text" name="text2"/></p>

Related

php returning a EMPTY echo. HOW / WHY?

I am having a very difficult time to figure out WHY I am getting this empty echo. I am debugging and I see all the values in the right place but it does not echo!
<body>
<div class='wrapper'>
<div id='message'>
<h2>Mail Sent Successfully!</h2>
<p>We will be in touch soon.</p>
<?php
// Echo session variables that were set on previous page
if (isset($_POST['first_name'])) {
session_unset();
session_destroy();
$_SESSION['name'] = isset ($_POST['first_name']) ? $_POST['first_name'] : "";
$_SESSION["favcolor"] = "Green ";
echo "name: " . $_SESSION['name'] . ".<br>";
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
}
?>
</div>
<form method="POST" id="myform" class="myform" title="Apply Title" action="<?php echo $_SERVER['PHP_SELF']; ?>" style="background-color:#FFFFFF;">
<fieldset>
<div>
<label class="title">First Name</label>
<input type="text" name="first_name" id='first_name' class="required" minlength="2" data-msg-required="Please enter your first name" maxlength='128' value="<?PHP if(isset($_POST['first_name'])) echo htmlspecialchars($_POST['first_name']); ?>">
</div>
<p id="invalid-first_name" class="error_msg"></p>
</fieldset>
<fieldset>
<div>
<label class="title">Your employer's company name.</label>
<input type="text" name="employer" id='employer'>
</div>
<p id="invalid-employer" class="error_msg"></p>
</fieldset>
<input type="button" name="submit" id="submit_app" class="sub" value="submit"/>
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src='http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js'></script>
<script src='http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js'></script>
<script type="text/javascript" src="formToWizard.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#myform").formToWizard({ submitButton: 'submit_app' });
jQuery.validator.setDefaults({
errorPlacement: function(error, element) {
error.appendTo('#invalid-' + element.attr('name'));
}
});
});
</script>
While debugging the code I can see all the value that just got typed. But once you submit the form it does not echo!
You are trying to echo non existent session. Put session_start() before assigning variables.
<?php
// Echo session variables that were set on previous page
if (isset($_POST['first_name'])) {
session_unset();
$_SESSION['name'] = $_POST['first_name'];
$_SESSION['favcolor'] = "Green";
echo "name: " . $_SESSION['name'] . ".<br>";
echo "Favorite color is " . $_SESSION['favcolor'] . ".<br>";
}
?>
EDIT:
I cleaned your code a little bit, try now. Dont destroy session, you can simply unset it if u need to. Also, im not sure what are you trying to do with $_SESSION['name'] = isset ($_POST['first_name']) ? $_POST['first_name'] : ""; so i changed that.

Get checkbox value with php

I try to determine if a checkbox is checked or not, but i get an error.
test.php
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Use Proxy : <input type="checkbox" name="use_proxy"><br><br>
<input type="submit">
</form>
<?php
$use_proxy = $_POST['use_proxy'];
if ($use_proxy != "on")
{
$use_proxy = "off";
}
echo "<p> use_proxy = " . $use_proxy . "</p><br>";
?>
</body>
</html>
I get this error:
Notice: Undefined index: use_proxy in C:\xampp\htdocs\mbcl\checkbox_test.php on line 11
How can i solve it?
This is the behaviour of checkbox until they are checked , they can not be fetched at backend(PHP) . You can try as below-
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Use Proxy : <input type="checkbox" name="use_proxy" value="off"><br><br>
<input type="submit">
</form>
<?php
$use_proxy = isset($_POST['use_proxy'])?"on":"off";
echo "<p> use_proxy = " . $use_proxy . "</p><br>";
?>
</body>
</html>

php get the hidden value after redirect the page

test.php
<?php
//CLICK SUBMIT BUTTON
if(isset($_POST['submit']))
{
$membername = $_POST['membername'];
$errors = '';
if(empty($membername))
{
$errors = "Please enter member name!<br />";
}
if($errors)
{
//MEMBER NAME TEXTFIELD EMPTY
//SHOW ERROR MESSAGE AND DISPLAY FORM AGAIN
echo '<span style="color:red;font-weight: bold;">'.$errors.'</span>';
displayForm();
}
else
{
//GO TO OUTPUT.PHP PAGE
header("Location:output.php");
exit();
}
}
else
{
displayForm();
}
?>
<?php
//DISPLAY FORM
function displayForm()
{
?>
<html>
<head></head>
<body>
<form action="test.php" method="post">
Member Name
<input type="text" name="membername" value="<?php if(isset($_POST['membername'])) echo $_POST['membername'];
else echo ''; ?>" /><br />
<input type="submit" name="submit" value="add" />
[HERE]
</form>
</body>
</html>
<?php
}
?>
In [HERE] section, I write the hidden input field:
<input type="hidden" name="mname" value="<?php echo $_POST['membername']; " />
After that, I go to output.php get the hidden field value:
<?php
echo $_POST['mname'];
?>
When I run the code, I get this error:
Undefined index: mname
What happened to my program?
header() function cannot applied to $_POST method?
Any solutions to solve it?
There Could be two solutions to get the value on redirected page :
1. By Session :
You can put the value in session and get on the redirected page.
$_SESSION['mname'] = $_POST['mname'];
2. Using GET :
You can send values in header.
header("Location:output.php?val=$_POST['mname']");

set value of input field by php variable's value

I have a simple php calculator which code is:
<html>
<head>
<title>PHP calculator</title>
</head>
<body bgcolor="orange">
<h1 align="center">This is PHP Calculator</h1>
<center>
<form method="post" action="phptest.php">
Type Value 1:<br><input type="text" name="value1"><br>
Type value 2:<br><input type="text" name="value2"><br>
Operator:<br><input type="text" name="sign"><br>
Result:<br><input type"text" name="result">
<div align="center">
<input type="submit" name="submit" value="Submit">
</div>
</form>
</center>
<?php
if(isset($_POST['submit'])){
$value1=$_POST['value1'];
$value2=$_POST['value2'];
$sign=$_POST['sign'];
if($value1=='') {
echo "<script>alert('Please Enter Value 1')</script>";
exit();
}
if($value2=='') {
echo "<script>alert('Please Enter Value 2')</script>";
exit();
}
if($sign=='+') {
echo "Your answer is: " , $value1+$value2;
exit();
}
if($sign=='-') {
echo "Your answer is: " , $value1-$value2;
exit();
}
if($sign=='*') {
echo "Your answer is: " , $value1*$value2;
exit();
}
if($sign=='/') {
echo "Your answer is: " , $value1/$value2;
exit();
}
}
?>
All I want to do is that answer should be displayed in the result input field instead of echoing them separately. Please help? I Know it's simple but I am new in PHP.
One way to do it will be to move all the php code above the HTML, copy the result to a variable and then add the result in the <input> tag.
Try this -
<?php
//Adding the php to the top.
if(isset($_POST['submit']))
{
$value1=$_POST['value1'];
$value2=$_POST['value2'];
$sign=$_POST['sign'];
...
//Adding to $result variable
if($sign=='-') {
$result = $value1-$value2;
}
//Rest of your code...
}
?>
<html>
<!--Rest of your tags...-->
Result:<br><input type"text" name="result" value = "<?php echo (isset($result))?$result:'';?>">
inside the Form, You can use this code. Replace your variable name (i use $variable)
<input type="text" value="<?php echo (isset($variable))?$variable:'';?>">
Try this
<input class="qtytext-box" type="number" value= <?php echo $colll2; ?> >

Embedding PHP into HTML

<?php
If ( isset ($_POST['name'] ) ) {
$name = $_POST['name'];
if (!empty ($name)) {
$sentence = $name . " is the best footballer of his generation. ";
} else {
echo "Please enter a name";
}
}
?>
<html>
<head>
</head>
<body>
<!-- ********************************** -->
<form action="form3.php" method="POST">
Name: <input type="text" name="name"><br>
<input type="submit" value="Send">
</form>
<textarea rows="7" cols="30"> <?php echo $sentence; ?> </textarea>
</body>
</html>
The code works just fine, but for some reason the text inside the textarea shows this error
Notice: Undefined variable: sentence in C:\xampp\htdocs\form3.php on line 29
Please help.
$sentence is only initialized when this statement is true: if (!empty ($name)) {.
To avoid the error, put $sentence = ""; above the if-statement.
You can solve this using different options:
1- define $sentence at top of the page such as:
$sentence = '';
2- or use isset($sentence) before printing it:
<?php echo isset($sentence)? $sentence : ''; ?>

Categories