This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
In this once the submit button is clicked it retrieve the data from database. But once i enter this page Undefined index: submit is shown before i click the submit button.
<div id="title">
<form method="get" action="<?php echo $_SERVER['PHP_SELF']?>">
<h3>Select your product and click submit</h3><br />
<select name="prod">
<option value="default">Select your product</option>
<option value="os">Operating Systems</option>
<option value="mobile">Smart Mobiles</option>
<option value="mobile">Computers</option>
<option value="shirt">Shirts</option>
</select><br /><br />
<input type="submit" value="Submit" name="submit"/>
</form>
The php code is:
<?php
$submitcheck=$_GET['submit'];
echo $submitcheck;
if (!isset($submitcheck)) {
echo 'Pls select and submit';
} else {
....
}
?>
You need to change your PHP to:
if (isset($_GET['submit']))
{
// Form has been submitted
}
else
{
// Form has not been submitted
}
At the moment, you're assigning a value which might not exist to $submitcheck and then checking whether it's set. You need to do it the other way round: check $_GET['submit'] is set, then assign it to a variable.
It's bacause you code is executed from top to bottom and on the top you are using $_GET['submit'] and then this key does not exist. You could do something like that:
if(array_key_exists('submit', $_GET)){
echo $_GET['submit'];
}
or even
if (!isset($_GET['submit']))
{
echo 'Pls select and submit';
}
else
{
....
}
Related
I have used a script to hide an input when an option value is selected. The form gets submitted but gives me an undefined index error for the variable which i have hidden. My script is as follows:
function Sbox()
{
if(document.getElementById("selectdrop").value=='2')
{
document.getElementById("text2").disabled=true;
}
}
and my form is a s follows:
<form action="" method="POST>
<select name="text1" id="selectdrop" onchange="return Sbox();">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="text" id="text2" name="text2"/>
</form>
when I select option 2, textbox2 is disabled. But when I submit the form , my data is being inserted but is showing an error of undefined index for text2.
My PHP code for submit is as follows:
if(isset($_POST['submit']))
{
$text1=$_POST['text1'];
$text2=$_POST['text2'];
$sql="INSERT INTO letters (text1 , text2) VALUES ('$text1' , '$text2')";
if(mysqli_query($connect , $sql)==true){
echo "data has been successfully entered";
}
else{
echo "data not entered";
}
}
You are disabling text2. Try using following code and it should work fine.
function Sbox()
{
if(document.getElementById("selectdrop").value=='2')
{
var s = document.getElementById("text2");
s.setAttribute("type", "hidden");
}
}
Now you will be able to get the value of text2 in PHP as its hidden and not disabled.Disabled element's value does not get POSTED.
Hope it helps.
Thanks.
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'];
}
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 using rateit rating plugin for my website (https://rateit.codeplex.com/).
I'm trying to insert the value that a user selects into my database however every time I do this value 0 is inserted. Here is my code (PHP first):
session_start();
$username = $_SESSION['username'];
if(isset($_SESSION['username'])) {
$username = $_SESSION['username'];
}
else {
header('Location: login.php');
}
$itemid = $_GET['id'];
(HTML):
<div id="itemreview">
<form action="" name="review" id="review" method ="post">
Skin Type: <select name="skintype">
<option selected="">Skin Type</option>
<option value ="Oily">Oily</option>
<option value ="Mixed">Mixed</option>
<option value ="Sensitive">Sensitive</option>
<option value ="Normal">Normal</option>
</select><br>
Rating:
<input type="range" value="0" step="0.5" id="rating">
<div id="stars" class="rateit" onclick="" data-rateit-backingfld="#rating" data-rateit-resetable="false" data-rateit-ispreset="true"
data-rateit-min="0" data-rateit-max="10">
</div>
<br>
<textarea name="review" id="review" cols ="50" rows="8"></textarea>
<input type="submit" name="submitcomment" id="submit" value="Comment!">
</form>
Here's my Jquery/Ajax:
<script>
$(document).ready(function () {
$('#submit').click(function () {
//Create a variable to hold the value entered by the user
var stars = $('#stars').rateit('value');
$.ajax({
url: 'itemreview.php',
type: "POST",
data: { "stars" : stars },
success: function (data) {
alert("Got it!"+ stars);
}
});
});
});
</script>
And here's my php:
<?php
//Insert review into database
if(isset($_POST['submitcomment'])) {
$skintype = $_POST['skintype'];
$userrating = $_POST['stars'];
$userreview = $_POST['review'];
$insertreview = mysql_query("INSERT INTO itemcomments (itemid, username, commentcontent, ranking, userskintype) VALUE('$itemid', '$username',
'$userreview', '$userranking', '$skintype')");
echo "Posted!";
echo $userrating;
}
?>
While I can get the value displayed with the alert("Got it!"+ stars); I cannot get the correct value inserted into database when I click submit.
For example, I rate something for 7.5. After hitting submit I get the alert message "Go it!7.5" but in the database the value inserted is still 0. All other values ($skintype, $userreview, $itemid and $username) are inserted properly.
What might be the issue?
Thank you all for your help.
<input type="range" value="0" step="0.5" id="rating">
I think that you need to change the value to value = "" otherwise you'll always get the value zero. Try that ! And let me know. If that doesn't help than i'll look at the code again.
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'];
I am using forms select. I just want to check what user selects by echo-ing the result on the same page so I kept the action="". But its showing error undefined index slct. Can any one please help me
<form action="" method="post">
<select name="slct">
<option value="yes" selected="selected"> yes </option>
<option value="no"> no </option>
</select>
<input type="button" value="Submit" />
</form>
<?php
$tofd = $_POST["slct"];
echo $tofd;
?>
Why its showing the error
Notice: Undefined index: slct in C:\wamp\www\Univ Assignment\Untitled-4.php on line 21
You should use button type submit NOT button
<input type="submit" value="submit" />
And then test IT like
echo (isset($_POST['slct']))? $_POST['slct'] : 'Variable undefined..';
Use PHP isset to check if its exist first
Example :
$tofd = isset($_POST["slct"]) ? $_POST["slct"] : null ;
Example 2 Using a function
function __POST($var)
{
return isset($_POST[$var]) ? $_POST[$var] : null ;
}
$tofd = __POST("slct");
If they are on the same page, initaially, $_POST would be empty because the user has not posted anything. So you have to handle that.
if(isset($_POST["slct"]))
$tofd = $_POST["slct"];
<?php
if (isset($_POST["slct"])){
$tofd = $_POST["slct"];
echo $tofd; }
?>