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 searched the whole internet saw unlimited stack posts followed by letter some tutorials and still i cant get it working but why? it keeps telling me undefined index when i use isset becomes undefined variable. Sorry my coding level is low...
Thanks in advance.
<form name="ghostform" action="insert.php" method="post">
<input type="hidden" name="id" value="1" id="id" />
<input type="hidden" name="eidos" value="2" id="eidos" />
<input type="hidden" name="seidos" value="3" id="seidos" />
<input type="hidden" name="idiotites" value="4" id="idiotites" />
</form>
<?php
mysql_connect("localhost","root","")or die("cant connect to server");
mysql_set_charset('utf8');
#mysql_select_db("cafemanager")or die( "Unable to select database");
$id = $_POST['id'];
$eidos = $_POST['eidos'];
$seidos = $_POST['seidos'];
$idiotites = $_POST['idiotites'];
mysql_query("INSERT INTO Current(id, eidos, specificeidos, idiothtes) VALUES('$id','$eidos','$seidos','$idiotites')");
?>
Use something like this:
<?php
if($_POST) {
mysql_connect("localhost","root","")or die("cant connect to server");
mysql_set_charset('utf8');
mysql_select_db("cafemanager")or die( "Unable to select database");
$id = mysql_real_escape_string(strip_tags($_POST['id']));
$eidos = mysql_real_escape_string(strip_tags($_POST['eidos']));
$seidos =mysql_real_escape_string(strip_tags( $_POST['seidos']));
$idiotites = mysql_real_escape_string(strip_tags($_POST['idiotites']));
mysql_query("INSERT INTO Current(id, eidos, specificeidos, idiothtes) VALUES('$id','$eidos','$seidos','$idiotites')");
}
?>
<form action="" method="post">
<input type="hidden" name="id" value="1" id="id" />
<input type="hidden" name="eidos" value="2" id="eidos" />
<input type="hidden" name="seidos" value="3" id="seidos" />
<input type="hidden" name="idiotites" value="4" id="idiotites" />
</form>
But please start using mysqli not depracted mysql
Every time you need a GET or a POST, I'd recommend you to especify default values in case that you don't get all values, so the function will not throw any warning, or extrange things inside the SQL sentence.
Example:
In this case i define 0 as default.
$id = isset($_POST["id"])?$_POST["id"]:0;
And also, if you are waiting for a number as a value.
$id = isset($_POST["id"])?intval($_POST["id"]):0;
If 0 is an impossible value and don't makes any sense, then you know that something has gone wrong, and you can react to it.
how do you submit this?
and please use if(isset($_POST['name']){ // do stuffs }
The reason it shows "undefined index or variable" is because PHP cannot find the variables in the $_POST array.
If this page is handling inputs from a form, make sure that the action of the form points to this page and the method of the form is set as POST.
<form action="urlofthispage" method="POST">
If this page is also used for handling other scenarios, you can simply check if variables are set in $_POST. The isset function simply checks if the variable has been set. Like this:
if(isset($_POST['id']))
{
//then do what do u want if the variable is present
}
else
{
//then do what do u want if the variable is not present
}
For the official documentation of the isset function, you can check this for reference:
http://in2.php.net/isset
Your actual problem
You assume that PHP can get the values from your fields, which you can't..
Because you need to submit the values somehow to the server before PHP realizes that they are there, either do this by a submit button or send your values via javascript.
PHP
A server side language that is not executed until you request the content from the server. Meaning that if you want these values (that are located in the clients browser) you need to send them (POST) to the server for processing everything between <?php and ?>
Try this code
<form name="ghostform" action="insert.php" method="post">
<input type="hidden" name="id" value="1" id="id" />
<input type="hidden" name="eidos" value="2" id="eidos" />
<input type="hidden" name="seidos" value="3" id="seidos" />
<input type="hidden" name="idiotites" value="4" id="idiotites" />
<input type="submit" value="kablamo">
</form>
Tested the code, and it works.
As long as you have a submit button...
On a side note, here's how isset works
function getVal($var) {
if(!isset($_POST[$var]))
return 'default';
return $_POST[$var];
}
$id = getVal('id');
$eidos = getVal('eidos');
$seidos = getVal('seidos');
$idiotites = getVal('idiotites');
Use isset to catch if the value is set or not.
And you could use a function with default values (this is just a primal example) if the value is not set. Or you could raise some form of exception before connecting to the database.
#MiQUEL's solution is neater, only reason why I'd go with a function is because it's more readable and you can perform operations before setting the value.. like this:
function getVal($var) {
if(!isset($_POST[$var])) {
mysql_connect("localhost","root","")or die("cant connect to server");
mysql_select_db("mydb");
$result = mysql_fetch_array(mysql_query("SELECT defaultValue FROM mytable WHERE name=" . $var), MYSQL_NUM);
return $result[0];
}
return $_POST[$var];
}
Related
I have a simple code (very simple one) that I was using to try something out for a work and I was trying a function to work with the variables of a form radio in post method, to update my SQL table with the output of the form. But when I'm going to try it, it doesn't update and gives me a notice.
It has something to do with the query (because the error says is in that line of the code) but I still don't know what it is.
I tried to change the syntax of the SQL sentence in different ways. I changed the user I was going to use to change the "image_value" column. I even checked the syntax of the query in phpmyadmin, and it worked.
Here is the php code:
<?php
mysql_connect("localhost","root","");
function user_image($value){
print_r($value);
//This is the problem
$query = "UPDATE users SET image_value = '$value' WHERE (ID) = '6'";
mysql_query($query);
}
?>
And here i have the code of the form and how I'm using the function (if there is any mistake that I haven't seen)
<form method="post" action="">
<input type="radio" name="1" value="1">imagen1
<br>
<input type="radio" name="2" value="2">imagen2
<br>
<input type="radio" name="3" value="3">imagen3
<br>
<input type="radio" name="4" value="4">imagen4
<br>
<button type="submit"><span>Submit</span></button>
</form>
<?php
user_image($_POST);
?>
Your problem is you are passing the complete object $_POST.
You don't specify if your radio name is correct (The name of your radio as a number from 1 to 4).
In the case, you are trying to set the value of image_value from a radio button should be.
<form method="post" action="">
<input type="radio" name="image_value" value="1">imagen1
<br>
<input type="radio" name="image_value" value="2">imagen2
<br>
<input type="radio" name="image_value" value="3">imagen3
<br>
<input type="radio" name="image_value" value="4">imagen4
<br>
<button type="submit"><span>Submit</span></button>
</form>
<?php
if (isset($_POST['image_value'])) {
user_image($_POST['image_value']);
}
?>
and your function
function user_image($value) {
mysql_connect("localhost","root","");
print_r($value);
//This is the problem
$query = "UPDATE users SET image_value = '$value' WHERE (ID) = '6'"; //ID should be dynamic base on the user I guess
mysql_query($query);
}
?>
$_POST pass an object value to the server, you need to specify the property you want to use, var_dump $value to understand all what it contains.
Having issues with using a form's value in a different php file:
my firstpage.php
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
</form>
my secondpage.php is here
<?php
include("firstpage.php");
$result = $_POST['rdbbtn'];
if ($result == "1") {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
problem:
Notice: Undefined index: rdbbtn in
how come I can't use "rdbbtn"? Should I have something like
$rdbbtn = $_POST['rdbbtn'];
in secondpage.php? Tried this but didn't solve my problem.
firstpage.php and secondpage.php are in the same directory.
Probably it's some pretty obvious thing that I don't see...thanks!
EDIT: I have accepted pradeep's answer as that helped me the most to figure what the problem should be. would like to say thank you for everybody else showing up here and trying to help!
When you change current page it reset the value and $_POST is empty.
You can try with set form action to next page . It will work
<form method="post" action="secondpage.php">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="" value="Next">
</form>
Other wise you can make a function in a class and set each page action
to this function.
And set your each form data to session.
Finally when you change the page you read data form session.
Class FormAction{
public function setFormDataToSession(){
if(isset($_POST['rdbbtn']){
$_SESSION['rdbbtn'] = $_POST['rdbbtn'];
}
}
}
In your page simply get the session value.
echo $_SESSION['rdbbtn'];
Should be like this :
Check with isset method in
<?php
include("firstpage.php");
$result = isset($_POST['rdbbtn']) ? $_POST['rdbbtn'] : NULL;
if ($result == 1) {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
and your form should be like this :
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="submit" value="submit">
</form>
Sorry for not being able to comment in this post(less reputations). But seems like you are asking about storing the variables of the session. This way you can use the variables for a whole session. Just start the session by putting session_start() in the very beginning of secondpage.php file and then you can access the variables at any time during the session by simply calling $_SESSION['rdbutton] in any page like fourthpage.php or anything. Just make sure u put the session_start() at the top of each page where you want to use the variables. Don't forget the semicolons at the end. 😜 Hope this helps.
I have a value coming from another form in the same page called $_POST['serial']. And i want to use this value to run a query in another form but after I submit the second form nothing happened and the query not running.
<?php
if (isset($_POST['serial'])) {
$serial = $_POST['serial'];
?>
<form action="" method="post">
<button type="submit" name="submit">Click to use</button>
</form>
<?php
if (isset($_POST['submit'])) {
$query = mysql_query("UPDATE table_name SET status = 'inactive' WHERE serial = '$serial'");
}
}
?>
To pass the variable along you would create a hidden input on your second form to contain the value:
<?php
// check and clean up the passed variable
$serial = isset($_POST['serial']) ? htmlspecialchars($_POST['serial']) : '';
?>
<form action="" method="post">
<input type="hidden" name="serial" value="<?php echo $serial; ?>" />
<button type="submit" name="submit">Click to use</button>
</form>
For Safety's Sake
Your script is at risk for SQL Injection Attacks.
If you can, you should stop using mysql_* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really not hard.
Additional Thoughts
If you're planning to do a two-step form you'll likely want to place all of the data processing outside of the form page, in a separate PHP file. With the limited code that you have shown I fear that we will miss something in our answers which will lead you to additional questions because your code still isn't working as you would expect.
A button needs a name and a value to be successful. Your button doesn't have a value so $_POST['submit'] will be undefined.
Add a value attribute to your <button> element.
After you do that, $serial will be undefined because your form doesn't submit that.
You need to include it in your form too:
<input type="hidden" name="serial" value="<?php echo htmlspecialchars($serial); ?>">
Does anyone know why this code will not work? I simply want to print which radio button is selected. It always prints 'Null' no matter what is selected. PHP code is below.
<?php
$conn = mysql_connect('localhost','student','student') or die(mysql_error());
mysql_select_db('vgs',$conn);
//Get Question 1
if (isset($_GET["q1option"]))
{
$q1option = $_GET["q1option"];
}
else
{
$q1option = "Null";
}
//Process Question 1
echo "".$q1option;
The HTML code is below.
<form action="" method="get" >
<div id="Q1">
<label><input type="radio" name="q1option" value="Less_than_16" />Less than 16</label><br />
<label><input type="radio" name="q1option" value="16_or_more" />16 or more</label>
</div>
Any help with this would be greatly appreciated. Note I have many tables in the 'vgs' database, if that makes a difference.
Thank you,
Daniel
Additional Code
HTML below
<input type="button" value="Submit" onclick="result();" />
<input type="reset" value="Reset" />
</form>
Embedded JavaScript below. It uses http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js
function result()
{
$('#Suggestion').load('process_answers.php');
}
Looks like your submit button is outside of the <form> tag, it should be within the form:
<form action="" method="get" >
<div id="Q1">
<label><input type="radio" name="q1option" value="Less_than_16" />Less than 16</label><br />
<label><input type="radio" name="q1option" value="16_or_more" />16 or more</label>
</div>
<input type="submit" value="Submit" />
</form>
You are always getting Null in return because your query string is not being attached anywhere. If you are using AJAX, you need to pass the query string data. Your cycle is not working properly because of a misconfiguration.
Take the following code:
//Get Question 1
if (isset($_GET["q1option"]))
{
$q1option = $_GET["q1option"];
}
else
{
$q1option = "Null";
}
Using $_GET will attempt to extract from your query string a variable labeled 'q1option'. If this variable is not set, then return 'Null'. So your code is working as expected as it is currently structured.
If you want to pass the variable between pages you will either need to submit the form and the variable will be automatically passed, or you need to modify your jQuery to allow for the passing and processing behind the scenes of your variable. Once processed, you would then handle the results accordingly and render them to the client as desired.
// This is the file that is giving the error, not the form below
<?php
// Insert Comments into Database that user provides
<?php
// Insert Comments into Database that user provides
$comm = mysql_real_escape_string($_POST['addComment']);
// following line has changed:
$pID4 = filter_var( $_POST['pID'], FILTER_SANITIZE_STRING );
$cID = mysql_real_escape_string($_POST['courseInfoDD']);
$username = "###";
$password = "###";
$pdo4 = new PDO('mysql:host=localhost;dbname=###', $username, $password);
$pdo4->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth4 = $pdo4->prepare('INSERT INTO Comment (info, pID, cID) VALUES(?,?,?);');
$sth4->execute(array($comm, $pID4, $cID ));
?>
Form
<input type='text' id='addComment' name='addComment' tabindex='3' value='Enter comment' />
<input type='hidden' name='pID' value='<?php echo $pID ?>'>
</form>
ERROR Received:
*No error is received upon load, but once I type something in and press enter it gives me a blank page saying 'no pID specified' ?? Please help!*
To directly answer your question, you'll need to add the pID to the request data either via the form action, though this parameter will show in the $_GET array instead of $_POST
<form action="inc/q/prof.php?pID=<?php echo $pID ?>" method="post">
or via a form element (will be part of the $_POST array)
<input type="hidden" name="pID" value="<?php echo $pID ?>">
Now, a further consideration...
You don't need to apply db string escaping (mysql_real_escape_string()) when using PDO prepared statements with bound parameters. The act of binding a parameter or value takes care of that for you.
To clarify my comments below, you need something like this...
Given a URL like http://example.com/index.php?pID=842, your form on that page should have the following hidden element
<input type="hidden" name="pID" value="<?php echo (int) $_GET['pID'] ?>" />
Two words: GET FIREBUG. Before checking your PHP script, you should check your HTML form. It's possible you're not echoing the form correctly.
I don't thinks it's safer to go with POST submissions, but definitely it's cleaner.
After you checked your form it should look like this:
<form method="POST" action="form-process.php">
<input type='text' id='addComment' name='addComment' tabindex='3' value='Enter comment' />
<input type="hidden" name="courseInfoDD" value="XXX" id="courseInfoDD">
<input type="hidden" name="pID" value="XXX" id="pID">
</form>
On your submit script, you can access those parameters with $_POST. But remeber, if you have an empty value on your HTML form, it would become an empty variable.
You can do a quick echo on $pID to see their content.
#Phil Brown is right about PDO. You don't have to escape variables before sending it to the handler.
Hope it helps!