trying to make feedback page [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I am making feedback page, but it is producing some errors.
i don't understand what is wrong. Please help me figure out where i am doing wrong.
when i submit the form the errors goes away and produce right result,but when i refresh the page it produces same errors again
<?php
$name = $_POST['name']; // error undefined index name
$suggestion = $_POST['suggest']; // error undefined index suggest
$opinion = $_POST['opinion']; // error undefined index opinion
$submit = $_POST['submit']; // error undefined index submit
if(isset($submit)){
$sql= mysqli_query($con,"insert into feedback (name,suggestion,opinion) values ('$name','$suggestion','$opinion')");
if($sql==true){?>
<div class="alert alert-info">
Thankyou for your suggestions. we will notify the admins.
</div>
<?php
}
}
?>
<body>
<div class="feedback">
<form action="feedback.php" method="post">
<h1> Help us improve our website</h1>
<h2>Please drop your suggestion below</h2>
<div class="form-group">
<label >Your Name:</label>
<input type="text" class="form-control" name="name" required>
</div>
<div class="form-group">
<label for="comment">Your suggestion</label>
<textarea class="form-control" name="suggest" rows="5" id="comment" required ></textarea>
</div>
<p>Were you satisfy with this website?</p>
<label class="checkbox-inline">
<input type="checkbox" name="opinion"value="1"> yes
</label>
<label class="checkbox-inline">
<input type="checkbox" name="opinion" value="2"> no
</label>
<label class="checkbox-inline">
<input type="checkbox" name="opinion" value="3"> may be
</label>
<br /><br />
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</body>

When initially rendering the page, it's unable to locate the index (ie. value) for $_POST array (name, suggest, etc.). That's because there is no $_POST data until you submit the form. When you submit the form, $_POST contains data, including those indexes, so it's able to successfully render the page.
You should check to see if the variable has been set or not. Something along these lines will do that:
if (isset($_POST["name"]) {
$name = $_POST["name"];
}
You can do this for each variable.
I also recommend including that within the submit condition, as it should only check for that data if the form was submitted.
Additionally, you should look into using prepared statements or input sanitization as you are setting yourself up for SQL Injection with the mysqli query.

That's because parameters passed with post method are related only to a specific page request. When you refresh the page, browsers usually ask you if you want to send forms again, otherwise the request is sent without additional data.

Your issue is exactly what the error states, your $_POST array is empty when the page is first loaded, and it is not empty (so you do not get the error) when you have POSTED the form.
replace your variable declarations as follows to make sure they are properly declared incase the $_POST array is empty:
$name = isset($_POST['name']) ? $_POST['name'] : "";
$suggestion = isset($_POST['suggest']) ? $_POST['suggest'] : "";
$opinion = isset($_POST['opinion']) ? $_POST['opinion'] : "";
$submit = isset($_POST['submit']) ? $_POST['submit'] : "";
if(isset($submit) && $submit != ""){
// your insert code here
}

Related

PHP store html input in a global variable [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
i am building a multi step login page like that of gmail,
i want the email address the the user typed in to be stored in a global variable so i can echo it in the next step of the form i have been trying but can't figure it out, have seen many error, when i change the code i get a different one the new one i have now is (Notice: Undefined index: email in C:\xampp\htdocs\xxxxx\index.php on line 3)
this is the form
<form action="" method="post" class="form-login">
<div class="step-login step-one">
<input type="text" class="email"/>
<input type="button" class="btn next-step" value="next">
</div>
</form>
this is the php code that i am trying to use and store the input, i think this is where the problem is
<?php
$emails = $_POST['email'];
if(isset($_POST['next'])){
$GLOBALS['email'] = $_GET['email'];
}
?>
this is the code where i am trying to echo the varible
<div class="data-user-find">
<p class="user-email"><?php echo $GLOBALS['email']; ?></p>
</div>
Please guys help me
The "value" of your button is "next", but your button isn't called "next", it should be <button name="next" value="next" type="submit">
Then your error would go away. Also, you define an $email variable outside of your if-clause, please put it inside your if clause, so your code would be this:
<?php if(isset($_POST["next"]) {
$email = $_POST["email"]; (DONT FORGET TO GIVE YOUR MAIL INPUT THE 'name="mail"' TAG)
$GLOBALS["email"] = $email;
} ?>

How can I post form to mysqli without using an action page [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I don't wanna use an action page. I wanna post on the same page.
These are my codes. But there is an error called "Undefined index: baslik on line 5 and aciklama on line 6
<?php
include("baglan.php");
$site_basligi = mysqli_real_escape_string($conn, $_POST['baslik']);
$site_aciklamasi = mysqli_real_escape_string($conn, $_POST['aciklama']);
$ayarsql = "UPDATE ayar SET baslik='$site_basligi', aciklama='$site_aciklamasi' WHERE durum='1'";
if($conn->query($ayarsql)){
echo "Güncelleme başarılı";
}
?>
<form action="" method="post">
Site Başlığı: <input type="text" name="baslik" ><br><br>
Açıklama: <input type="text" name="aciklama"><br><br>
<input type="submit">
</form>
<?php
include("baglan.php");
// check for post request here
if(isset($_POST['baslik']) && $_POST['baslik'] != "") {
$site_basligi = mysqli_real_escape_string($conn, $_POST['baslik']);
$site_aciklamasi = mysqli_real_escape_string($conn, $_POST['aciklama']);
$ayarsql = "UPDATE ayar SET baslik='$site_basligi', aciklama='$site_aciklamasi' WHERE durum='1'";
if($conn->query($ayarsql)){
echo "Güncelleme başarılı";
}
}
?>
<form action="" method="post">
Site Başlığı: <input type="text" name="baslik" ><br><br>
Açıklama: <input type="text" name="aciklama"><br><br>
<input type="submit">
</form>
You are not checking if the POST data actually exists, so when you first load the page there is no $_POST
do something like
if (isset($_POST)){
//your php code here
}
I can see the form IS submitting to the same page.
You just should wrap the PHP code in if condition
<?php
include("baglan.php");
if(!empty($_POST['baslik'])) { // This is the new if condition
$site_basligi = mysqli_real_escape_string($conn, $_POST['baslik'] );
$site_aciklamasi = mysqli_real_escape_string($conn, $_POST['aciklama']);
$ayarsql = "UPDATE ayar SET baslik='$site_basligi', aciklama='$site_aciklamasi' WHERE durum='1'";
if($conn->query($ayarsql)) {
echo "Güncelleme başarılı";
}
}
?>
<form action="" method="post">
Site Başlığı: <input type="text" name="baslik" ><br><br>
Açıklama: <input type="text" name="aciklama"><br><br>
<input type="submit">
</form>

HTML - method POST in form does not send data in phpstorm [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I have this issue that my form obviously doesn't send data with POST method but it sends it with GET method.
Here is my HTML code of the form
<form action="action.php" method="POST">
<input type="text" name="text">
<input type="submit" value="send">
</form>
and here is the php code in the action page
if($_SERVER['REQUEST_METHOD'] == 'POST'){
echo $_POST['text'];
var_dump($_POST);
}
if(isset($_POST['text'])){
echo "ok";
}else{
echo "no";
}
when I submit the form I get this error for output
Notice: Undefined index: text in F:\test\action.php on line 9
array(0) { } no
but when I send data with GET method it works correctly without any problem.
I think the problem is for phpstorm because it runs correctly in the xampp server. and the considerable thing is when I run it in mozila or IE it says page not found but xampp is okay.
Try using isset with your input like so:
You have to add name="something" for the isset to pick up that you have clicked it.
<?php
if (isset($_POST['sub']))
{
echo $_POST['text'];
}
?>
<form action="" method="post">
<input type="text" name="text">
<input type="submit" name="sub" value="Submit">
</form
I can only assume that the output you are seeing is before you submit the form. When you submit it, you should check for POST and not for post:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
^^^^ here
echo $_POST['text'];
}

SESSIONS: using a user input variable for multiple php pages [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I'm trying to study php and I'm already on the sessions part where I want to input something on my first page that would then be an output for the second page
1stpage.php
<?php session_start();?>
<form method="post">
Enter a number: <input type="number" name="num1" min="1" max="20" value="<?php echo $_SESSION["$num1"];?>">
<a href ="2ndpage.php">
<input type="button" name="select" value="select">
</a>
</form>
2ndpage.php
<?php
session_start();
echo $_SESSION[$num1];
?>
Well, it does'nt work and I'm getting lots of undefined index error. Any fix guys? Thanks in advance.
Take a look at form handling:
http://www.w3schools.com/php/php_forms.asp
If you still want to save the value into a session use:
$_SESSION['num1'] = $_POST['num1'];
1stpage.php
<?php session_start();?>
<?php if(isset($_SESSION['num1'])) $num1 = $_SESSION['num1']; else $num1 = ''; ?>
<form method="post" action="2ndpage.php">
Enter a number: <input type="number" name="num1" min="1" max="20" value="<?php echo $num1;?>">
<input type="submit" name="select" value="select">
</form>
2ndpage.php
<?php
session_start();
$_SESSION['num1'] = $_POST['num1'];
echo $_SESSION['num1'];
?>
What I have done here is I have first looked into the session if the num1 offset has been set into the session. If its not then I give $num as blank else I set the assign the value from session to $num.
Now when we input something and post it on 2nd page, the posted value is assigned to the variable in session and is displayed as well. So that next time you visit 1stpage.php while in same session, you will see your last posted value.
Let us know if it solves your practive problem or if this is not what you wanted.
I believe this is what you're trying to do:
Page 1:
<?php
session_start();
if(isset($_POST['select'])){
$num1 = $_POST['num1'];
$_SESSION['num1'] = $num1;
}
?>
<form method="post">
Enter a number: <input type="number" name="num1" min="1" max="20" value="<?php echo $_SESSION['num1']; ?>">
<input type="submit" name="select" value="select">
Click here to See the Value
</form>
Page 2:
<?php
session_start();
echo $_SESSION['num1'];
?>
Where, at first you press the select to enter the value inside
num1 then click on Click here to See the Value to see it.
I don't think you understand how all this works. You're having PHP output a session variable into a text box and expecting that that text box will somehow magically update your session. Somewhere you have to pass that data back to PHP and update it. So let's make your code into a process
<?php session_start(); ?>
<form method="post" action="process.php">
Enter a number: <input type="number" name="num1" min="1" max="20" value="<?php echo $_SESSION["$num1"];?>">
<input type="submit" name="select" value="select">
</form>
What I've done is make this form do a basic POST. If you want to avoid this you can use AJAX instead (same result using JS instead of HTML)
Next, let's make a basic process.php intermediate page
<?php
session_start();
$_SESSION['num1'] = (int)$_POST['num1'];
header('Location: 2ndpage.php');
Now we have a logistics chain for the data. You accept the data in your HTML and POST it to this page. It then takes it, sanitizes it (note I cast it to an (int) so we are certain this is a number) and then issues a redirect to the browser which takes you to your original landing page, which should now work.
This is all oversimplified for the process of teaching. You could have your form submit directly to 2ndpage.php and process there. Or, as I said before, you could use AJAX to send to process.php and when it returns some success parameter you then use JS to redirect. You have many options here.
Put this on the first page:
$_SESSION['num1'] = $num1;
and this on the second
$num1 = $_SESSION['num1'];

Undefined Index in php (I've searched all over internet, and stackoverflow, tried almost everything) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
enter code herethis is php code
$choice_spc_port = $_POST["txtSpace"]; (THIS LINE GIVES UNDEFINED INDEX NOTICE)
$choice_loc = $_POST["txtLocation"]; (THIS LINE GIVES UNDEFINED INDEX NOTICE)
And this is HTML Form
<form id="animalform" name="animalform" method="post" action="animalform.php">
<div class="wrapper"> <strong><span>*</span> Text Space portion:</strong>
<div class="formText">
<input type="radio" name="txtSpace" value="HR"/>Text space for Horse.<br />
<input type="radio" name="txtSpace" value="ZB"/>Text Space For Zebra<br />
</div>
</div>
<div class="wrapper"> <strong><span>*</span> Animal Location:</strong>
<div class="formText">
<input type="radio" name="txtLocation" value="txtSetXY"/> Specify Animal Location<br />
<div style="padding-left:20px;">
X: <input type="text" id="locField" name="txtXLocation"> <span>Taking (x=1,y=1) top left box. Increment +1 in value of X moving right</span><br />
Y: <input type="text" id="locField" name="txtYLocation"> <span>Taking (x=1,y=1) top left box. Increment +1 in value of Y moving downwards</span><br />
</div>
<input type="radio" name="txtLocation" value="Default"/>Default
</div>
</div>
</div>
Query to insert in database
$insert = "INSERT INTO dbForm (db_space_portion, db_animal_location) VALUES ('{$choice_spc_port}', '{$choice_loc}')";
I tried the same in firefox and those two lines give notice as mentioned in bracket. Also on internet many people are looking for solution. So I think this question will help many out there.
This error shows in newer version of PHP.
There are various way to remove the error:
You can use error suppress method for this
Check that variable is not empty or set it to 0 e.g. choice_spc_port=0 at the start
Isset method also works good
Use this query.
if(isset($_POST['txtSpace'])) {
$choice_spc_port = $_POST['txtSpace'];
}
if(isset($_POST['txtLocation'])) {
$choice_loc = $_POST['txtLocation'];
}
$insert = mysql_query("INSERT INTO dbForm (db_space_portion, db_animal_location) VALUES ('{$choice_spc_port}', '{$choice_loc}')");
use isset for this, it will not give you undefined index notice when the $_POST['txtSpace'] and $_POST['txtLocation'] are empty and not set.
if(isset($_POST['txtSpace']))
{
$choice_spc_port = $_POST["txtSpace"];
}
if(isset($_POST['txtLocation']))
{
$choice_loc = $_POST["txtLocation"];
}
Do this to get any Post variables:
if(isset($_POST['txtSpace'])){$choice_spc_port = $_POST["txtSpace"];}
if(isset($_POST['txtLocation'])){$choice_loc = $_POST["txtLocation"];}
And to your SQL sentence:
$insert = "INSERT INTO dbForm (db_space_portion, db_animal_location) VALUES ({'$choice_spc_port'}, {'$choice_loc'})";

Categories