PHP $_Post variables print but still give index errors? - php

Hey, I've got what has become an extremely frustrating problem with $_Post variables. I'll give examples of code rather than the actual segments to save time and confusion. On one page I'm doing this:
<? echo "<form name='form' action='page.php' method='post'>
<input type='hidden' name='slot' value=".$i.">
</form>";
?>
The $i is an index in a while loop (I'm echoing this simple form several times). The form itself is submitted with a bit a javascript.
All's well at this point, the form is submitted properly and takes me to another page, where I need to use that "slot" value to do some other junk. However, when I try to do this:
<? echo "<form name='another_form' action='another_page.php' method='post'>
<input type='hidden' name='slot_num' value=".$_POST['slot'].">
//SOME OTHER HIDDEN VARS
</form>";
?>
or this...
<? echo "<form name='another_form' action='another_page.php' method='post'>
<input type='hidden' name='slot_num' value=";
echo $_POST['slot'];
echo ">
//SOME OTHER HIDDEN VARS
</form>";
?>
or this...
<? //TOP OF PAGE
$slots = $_POST['slot'];
?>
<? //FURTHER DOWN
<? echo "<form name='another_form' action='another_page.php' method='post'>
<input type='hidden' name='slot_num' value=".$slots.">
//SOME OTHER HIDDEN VARS
</form>";
?>
...all I get is an Undefined index: slot etc.. etc... error, and source of the php document just has blank space. Funny thing is, if I simply do this:
echo $_POST['slot'];
at the top of the page, it prints out the value from the previous page just fine, however, if I view the source, it still shows an Undefined index error instead of the value. I KNOW the value is getting passed because it prints, but I can't use it for anything else because if I try to include it in my php code, it just displays an error and gives a blank value!
I've also tried using $HTTP_POST_VARS['slots'] with the same result... I am at wits end after several hours of experimentation... any advice?

check for emptiness:
if(empty($_POST['foo'])) {
$foo = "default foo";
} else {
$foo = $_POST['foo'];
}
print "My foo is '$foo'";

Edit:
Based on your comments and posted code, you seem to be trying to echo $_POST['slot'] when you should be echoing $_POST['slots']... note the s at the end.
Since $_POST is a super global, it is available anywhere on your page, so your code should work.
I noticed that you mixed slot and slots as the index in you post (you wrote $HTTP_POST_VARS['slots'] as the last example and $_POST['slot'] everywhere else). Could that be the reason?
To check what $_POST looks like, try this right about where you want to print the hidden value (though it should work the same anywhere on your page):
<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
Also, your slot isn't being echoed with quote marks around it, so it should be:
<?php echo "<form name='another_form' action='another_page.php' method='post'>
<input type='hidden' name='slot_num' value='".$_POST['slots']."'>
//SOME OTHER HIDDEN VARS
</form>";
?>

Well I can't see anything wrong apart from a few syntax problems... okay so first of all, can you post me your javascript so I can see that.
Now instead of what you are doing with that, try this code:
?> <form name="another_form" action="another_page.php" method="post">
<input type="hidden" name="slot_num" value="<?php echo isset($_REQUEST['slot'])?$_REQUEST['slot']:'not found'; ?>">
</form>
<?
I am not fully sure what is wrong but i don't think the problem is in the code you've posted. it's probably sitting elsewhere so keep sending through code.

Try replacing...
<? //TOP OF PAGE
$slots = $_POST['slot'];
?>
with..
<?php //TOP OF PAGE
isset($_POST['slot']) : $slots = $_POST['slot'] ? $slots = '';
?>
Best of luck, hope that helps!

Related

use an isset result inside another one

I have a problem when developping a page in php, when click on a submit button another submit button. appear.
my problem appeared at the isset of this submit button, echo $b doesn't work; here is my code:
<form method='POST'>
<input type="submit" name="s1">
</form>
</body>
</html>
<?php
if(isset($_POST["s1"])){
$b=2;
echo "<form method='POST'><input type='submit' name='s2'></form>";
if(isset($_POST["s2"])){
echo $b;
}
}
I've already tried to make $b a global variable, but no change :(
Thank's for your help.
The flow of your code is first send the value of input s2, if this was set with some value echo your second form and set b variable to 2, when your second form is printed out on the browser the b value will have lost, so your must store this value in some place, this could be done through a hidden field in your second form like this:
echo "<form><input type='text' name='s2'><input type='hidden' name='b' value='$b'></form>"
if(isset($_POST["s2"])){
echo $_POST['b'];
}
Hope this helps you!

Input type image is not working in firefox

I have some problem.
Firefox can't read the value and name of input type image.
Here is my piece of code:
*blabla.php
<?php
echo"
<form action='cek.php' method='POST'>
<input type='image' src='something.png' alt='Submit button' value='Continue' name='noi'>
</form>
";
?>
*cek.php
<?php
echo"
$a = $_POST['noi'];
<input type='text' value='$a'>
";
?>
but Firefox says there is no input named 'noi'.
Please help me to solve this problem.
Thanks in advance.
What you need to do and am under the impression you want the word "Continue" to appear in the text box.
At least that's the impression I am getting by the use of value='$a' which tells me that you wish to echo that variable from your form.
If so, then you need to modify your PHP handler to:
<?php
$a = $_POST['noi'];
?>
<input type='text' value='<?php echo $a ?>'>
and adding PHP tags while echoing the value for it, using:
value='<?php echo $a ?>'
as opposed to: (which will cause a parse error)
echo"
$a = $_POST['noi'];
<input type='text' value='$a'>
";
since there is a semi-colon after $_POST['noi'] then trying to inject HTML into without going out of and back into PHP. A semi-colon tells PHP to stop. Yet since there was no closing PHP tag following the semi-colon and no re-opening tag, PHP throws that (parse) error.
That is the reason you are getting the undefined error message.
Having error reporting "ON", would have signaled this "parse" error.
Place the following at the top of your file(s) during development.
error_reporting(E_ALL);
ini_set('display_errors', 1);
which will signal errors, if found in your code.
Footnote:
Alternatively, you can use:
<?php
$a = $_POST['noi'];
echo"
<input type='text' value='$a'>
";
?>
simply by moving the $a = $_POST['noi']; outside the echo.
if you want to send "Continue" to the *cek.php script, you can use an html input element with type hidden and value "Continue" in form on *blabla.php
Submit button with type image only send x and y co-ordinates of the button (NOT its value).
So, in your *blabla.php:
<?php
echo"
<form action='cek.php' name='myform' method='POST'>
<input type='text' name='first-name' id='first-name' value='' />
<input type='hidden' name='my-input' value='Continue' />
<input type='image' src='submit.png' alt='Submit button' value='Continue' name='noi'>
</form>
";
?>
and in *cek.php
<?php
$a = $_POST['my-input'];
echo "<input type='text' value='$a'>";
?>
When using input type=image you are telling the browser to submit the x and y coordinates where the user clicked on the image. This allows you to add image map type functionality to your forms. Specification says:
“the element's value attribute must be omitted“
But browsers are inconsistent about that, some browsers like Chrome will send it, and some like Firefox follow the specifications and will send only the X and Y coordinates. That is the reason why there is no input named 'noi' in your example.
To learn how to inspect variables check the PHP documentation for functions like var_dump, var_export, print_r, isset etc. Put this code in cek.php, submit the form and you will see the $_POST array values.
<?php
var_dump( $_POST );
// Firefox:
// array(2) { ["noi_x"]=> string(3) "134" ["noi_y"]=> string(2) "91" }
// Chrome:
// array(3) { ["noi_x"]=> string(3) "121" ["noi_y"]=> string(2) "93" ["noi"]=> string(8) "Continue" }
?>

Why is global scope $_GET not seen?

One of my pages (video.php) is opened using form action as follows:
<?php
//Lots of code, including a WHILE loop
echo "<form action=\"video.php?id=".$row['id']."\" method=\"post\" target=\"_top\">
<input type=\"image\" src=\"".$image."\" style=\"width:180px;height:120px\"
alt=\"Submit\"></form>";
?>
On the page video.php?id, I get the id as follows and declare other global scope vars. However, why is the $_GET variable not seen in my echoed alert when I submit a form as in the following simplified code?
//video.php?id page
<?php session_start();
include 'connect.php';
$Vid = mysqli_real_escape_string($_GET['id']);
$login_id = mysqli_real_escape_string($_SESSION['login_id']);
if (isset($_POST['sample'])) {
echo "<script>
alert('$Vid');
</script>";
}
else//etc.
?>
<html><head></head><body>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" id="Form">
<button name="button" type="submit">Click</button>
<input type="hidden" name="hidden" value="sample">
</form>
</body></html>
When I alert $Vid, nothing is alerted (blank alert box). Obviously, I see the SESSION variable when I alert $login_id. Am I missing something with the $_GET? Is there any way for the global var $Vid to be recognized? If I could use $Vid it would save me 5 or 6 queries based on how my code is currently written.
This how you can correct
Put $row['id'] inside a hidden text box with name as "id"
Your form method is POST, so use $_POST to grab the data in the POST file.
I think you escaped wrongly and thus the id is not appended (notice the backslash after $row['id']?), try the following:
echo '<form action="video.php?id='.$row['id'] . '" method="get" target="_top">
<input type="image" src="' . $image . '" style="width:180px;height:120px"
alt="Submit"></form>";
Imho your coding style is very unreadable with all those backslashes. It's okay to mix single/double quotes where needed…
[edit] and you obviously need to change the method to "get". ;-)
Changed action from $_SERVER['PHP_SELF'] to action="" and now the GET variables are seen.

php, form using the same page after submittion

I'm wondering what's the easiest way to make let's say a form with user/pass and submit button with php but after you submit it goes back to the same page instead of going to another php page.
I'm thinking of if/else statement but a bit confused how to set it after lots tries but still not getting the result wanted
weird I did all those you guys said before I posted..but...>.<"
let's say just something simple like this...
but I also want to set if nothing is entered then sumbit is clicked there'll be an error....should be something easy but I don't know why I can't seem to figure it out
<?php
function userPass()
{
echo "<form method='post' action=" . $_SERVER['PHP_SELF'] . ">";
echo "<input type='text' name='user' /><br/>";
echo "<input type='text' name='pass' /><br/>";
echo "<input type='submit' value='Login' />";
}
if(empty($_POST["user"]))
{
userPass();
}
if(!(empty($_POST["user"])))
{
if($_POST["user"] == "comp")
{
echo "Welcome comp";
}
else
{
echo "Wrong user";
}
}
?>
The other answers are right; you only need to send the user back around to your current page in the "action" property. You can test to see if they did so using "isset".
Something that I'm surprised hasn't been mentioned yet is that your input is not being sanitized, and you're setting yourself up for disaster. Huge injection vulnerability in your action attribute:
$_SERVER['PHP_SELF']
If you don't sanitize this, then someone can just modify the URL that they see and your poor PHP script won't know any better than to process that as your SELF constant.
In other words, you absolutely must use an htmlspecialchars() function to html-encode that parameter. With that, your action should look more like htmlspecialchars($_SERVER['PHP_SELF']).
if current page is index.php, use index.php in form tag as value of action.
like this:
<form action="index.php" method="post">
</form>
u can check for submitted form by putting:
if(isset($_POST)){
...
}
at top of page
Just use below syntax
<form method="post" action="">
You can check whether post is set using isset() method.
<?php function userPass() {
echo "<form method='post' action=" . $_SERVER['PHP_SELF'] . ">";
echo "<input type='text' name='user' /><br/>";
echo "<input type='text' name='pass' /><br/>";
echo "<input type='submit' value='Login' />"; }
if(empty($_POST["user"]))
{
userPass();
}
*if(!(empty($_POST["user"])))*
{
if($_POST["user"] == "comp")
{
echo "Welcome comp";
}
else
{
echo "Wrong user";
}
}
?>
This part of your code is wrong, you should type : if(!empty($_POST["user"]))
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
This is exactly how you work with your form to reload the page when click the submit button inside this form.
ADDITIONAL:
Add required to all input you wanted to be required or check if it's empty.
This is a code that I created to control learning the required input fields satisfy the requirements. when this is so, the data will be sent to the database. if it does not meet the requirements, there will be a message may be shown at the top of the page
<?php
if (!isset($_POST['submitform'])) {
}
else
{
$_SESSION['firstname'] = $_POST['firstname'];
$_SESSION['lastname'] = $_POST['lastname'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['mobile'] = $_POST['mobile'];
$_SESSION['telephone'] = $_POST['telephone'];
$_SESSION['place'] = $_POST['place'];
$_SESSION['street'] = $_POST['street'];
$_SESSION['housenumber'] = $_POST['housenumber'];
$_SESSION['gender'] = $_POST['gender'];
if (empty($_POST['firstname']) or empty($_POST['lastname']) or empty($_POST['email']) or empty($_POST['mobile'])or empty($_POST['telephone']) or empty($_POST['place'])
or empty($_POST['street']) or empty($_POST['housenumber']) or !isset($_POST['gender']))
{
echo "Sending denied";
}
else
{
require 'database.php';
header('Location: succes.php');
} ?>
I hope this is helpful information for you
this code will help you
<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post">
or you could just do this:
<form action="" method="post">
Thank you ....
You can define in the form submission:
<form method=get action=index.php >
You can also leave action out altogether, and it will automatically post/get to that same file.
I think that all have missed the actual question, i.e. if there is a way to stay in the same page after submitting a form. The answer is NO. Once you submit a form -- whether you use the POST or GET method, and of course assuming that you are using $_SERVER['PHP_SELF'] or empty action, etc. -- a new page is opened automatically. This holds at least for PHP. (I have tried a lot of different ways using XAMPP.) I don't now about other scripting languages.

Passing data between PHP webpages from a dynamically generated list

I have a PHP code which generates a dynamic list inside a form like the following, note that the list is built dynamically from database:
echo '<form name="List" action="checkList.php" method="post">';
while($rows=mysqli_fetch_array($sql))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='hidden' name='SessionID' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
What I need is to POST the data corresponding to the user choice when he clicks on the button for that row to another page.
If we use hyperlinks with query strings there will be no problem as I'll receive the data from the other page using a GET request and the hyperlinks would be static when showed to the user.
Also I need to obtain the user input from a textbox which is only possible with POST request.
Simply from the other page (checkList.php) I need these data for further processing:
$SessionID=$_POST['SessionID'];
$Code=$_POST['code'];
As I have a while loop that generates the fields, I always receive the last entry form the database and not the one corresponding to the line (row) that the user chosed from the LIST.
I'm going to recommend that you clean up the names of variables so that your code can
at least tell us what it's supposed to do. It should be rare that someone looks at your code
and has a lot of trouble trying to see what you're trying to accomplish :P, ESPECIALLY when you need help with something ;]. I'm going to try some things and hope that it makes doing what you want easier to comprehend and perhaps get you your answer.
It's good to try your best to not echo large amounts of HTML unnecessarily within a script , so firstly I'm going to remove the
echos from where they are not necessary.
Secondly, I'm going to use a mysql function that returns an easier to process result.
$user = mysqli_fetch_assoc($sql)
Third, I don't know if form having a name actually does anything for the backend or frontend of php, so I'm
just going to remove some of the extra crust that you have floating around that is either invalid HTML
or just doesn't add any value to what you're trying to do as you've presented it to us.
And yes, we "note" that you're building something from the database because the code looks like it does =P.
I'm also sooo sad seeing no recommendations from the other answers in regard to coding style or anything in regard to echoing html like this :(.
<?php while($user = mysqli_fetch_assoc($sql)): ?>
<form action="checkList.php" method="post">
<input type='password' name='code' value='<?php echo $user['code'] ?>' />
<input type='hidden' name='SessionID' value='<?php echo $user['id'] //Whatever you named the field that goes here ?>' />
<input type='submit' value='Take Survey' />
</form>
<?php endwhile; ?>
i not sure this is correct
echo '<form name="List" method="post">';
while($rows=mysqli_fetch_array($result))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='button' value='Take Survey' onclick=show($rows[0])>";
echo "<br>";
}
and javascript
<script>
function show(id)
{
alert(id);
window.location="checkList.php?id="+id;
}
</script>
On checkList.php
$id=$_GET['id'];
echo $id;
You can just check in checkList.php whether $_POST['code'] exists and if exists retrieve $_POST['SessionID'] which will be generated from database. But one thing, if You have all hidden fields in one form, they all will be sent, so You need to think how to correct that - maybe seperate forms for each hidden field, submit button and maybe other POST fields.
And afterwards, You will be able to get data in the way You need - $SessionID=$_POST['SessionID'];
I suppose it is the easiest way to solve that.
You can try something like this:
while($rows=mysqli_fetch_array($sql))
{
$index = 1;
echo "<input type='password' name='code' id='code'>";
//Attach $index with SessionID
echo "<input type='hidden' name='SessionID_$index' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
On checkList.php
<?php
$num = explode('_', $_POST['SessionID']);
$num = $num[1];
//in $num you will get the number of row where you can perform action
?>
$form = 1;
while($rows=mysqli_fetch_array($sql))
{
echo '<form name="List_$form" action="checkList.php" method="post">';
echo "<input type='password' name='code' id='code_$form'>";
echo "<input type='hidden' name='SessionID' id='SessionID_$form' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
echo '</form>';
$form++;
}

Categories