EDIT: Thanks for all the helpful answers, I got it solved.
I can't really seem to find an answer to this question, and it's probably really simple.
I was just creating a page for fun where you can guess a number from 1-10. Person 1 enters a secret number, that person 2 will guess. However, I have had a lot of problems storing the $_POST from the secret number.
TL;DR I can't store the information of secretNumber into guessnumber_guessed.php file. For example look in guessnumber_guessed. First part of the if statement, if the inputNumber equals the secretNumber, it should say correct. Problem is, the variblae is undefined, how to a 'transfer the info'?
Hope you guys get my point, help is really appreciated
Here's the code:
guessnumber_welcome:
<form method="post" action="guessnumber.php">
<input type="text" name="secretNumber" placeholder="Type the secret number">
<input type="submit" name="submit" value="Send">
</form>
guessnumber.php:
<form method="post" action="guessnumber_guessed.php">
<input type="text" name="inputNumber" placeholder="Guess the secret number">
<input type="submit" name="submit" value="Guess!">
</form>
<?php
$secretNumber = $_POST["secretNumber"]
?>
guessnumber_guessed.php:
<form method="post" action="">
<input type="text" name="inputNumber" placeholder="Guess the secret number">
<input type="submit" name="submit" value="Guess!">
</form>
<?php
$inputNumber = $_POST["inputNumber"];
if ($inputNumber == $secretNumber) {
echo "<p id=\"correctAnsw\"> CORRECT! </p>";
}
else if ($inputNumber == 2) {
echo "<p id=\"wrongAnsw\">You're very close. Go up a little!</p>";
}
else if ($inputNumber==4) {
echo "<p id=\"wrongAnsw\">You're very close. Go down a little!</p>";
}
else if ($inputNumber > 10) {
echo "<p id=\"wrongAnsw\">The number is you guessed is too high. Stay within the borders!</p>";
}
else if ($inputNumber < 1) {
echo "<p id=\"wrongAnsw\">The number is you guessed is too low. Stay within the borders!</p>";
}
else {
echo "<p id=\"wrongAnsw\">This is not the number. Try a new one!</p>";
}
?>
The sessions will not help. They're user oriented. If the first person is using the page to enter a number and the second person comes after that, in the same session, on the same browser, you can use the sessions mechanism.
If you're trying to make a multi-user "game" and the two persons are with separate browsers, it means you must:
Pair the two persons somewhat (maybe a room mechanism)
Use some kind of server storage or cache (you can even use memcached for in-memory storage) to match the two persons and their answers.
I faced a similar problem trying to pass beetween two scripts some url strings for a "two-step" uploader.
In my opinion there is two solutions, depends on the level of security you want to have:
In guessnumber.php put the $_POST['secretNumber'] value in an input type="hidden"
<input type="hidden" value="<?php echo $_POST['secretNumber']; ?>">
In this way the value will be passed to the second script via POST and will be available in the $_POST array.
This method, is not safe for sensible datas, because everybody who can access the html source simply through the browser devtool can read or modify it!!
The second, and more safe, solution is to use the php session
In guessnumber.php start the php session and save the value in this way:
if ( !session_id() ) {
session_start();
}
$_SESSION['secretNumber'] = $_POST['secretNumber'];
then in guessnumber_guessed.php recover the session and get the value from there
if ( !session_id() ) {
session_start();
}
$secretNumber = $_SESSION['secretNumber'];
I strongly recommend the second solution.
Hope it helps :)
Related
I'm using a form to post data and collect it with PHP by using individual cookies after some validation..
it takes too much time to do things with the data which gets extended for more and more forms... I was thinking about improving my code with loops, functions or arrays, offered to create a map array...
share your suggestion on how should I re-write my code for more data and multiple forms which get the data exported to documents..
<?php
if(isset($_POST['send']))
// name
if(strlen($_POST['name']) < 2){
echo "error - name is too short";
}
// other validations with elseif..
else {
setcookie('001',$_POST['name']);
}
// age
if((isset($_POST['age']) && is_numeric($_POST['age']))){
setcookie('002',$_POST['age']);
}
?>
<form method="post">
<input name="name" type="text"/>
<input name="age" type="number"/>
<input type=submit" name="send" value="submit">
</form>
I think should store it as a key val pair in $_SESSION. You can infinitely add more key-val pairs without worrying much. Also it will be secure as the data won't be exposed in cookies.
I have three pages. One of which there is a list of texts the user can select. Upon clicking on one of the texts they will be redirected to another page by using:
<a href='second.php?text=whatever>Whatever</a>
A page where they will input the username they wish to send those texts to - using forms. I wish to proceed to the third page with those two variable - texts and username. I only manage to proceed to third page with username only.
I am getting third.php?username=inputtedUsername.
I want to get third.php?username=inputtedUsername&&text=whatever.
I am aware that I can do by storing the text to a SESSION on page two and than transfer it over to third page.
I wish to know if there is another secure way to do this - maybe something needed to be changed in the form action=thirdpage.php? I dont know. Thank you. ö.ö.
Solved: After reading comments and answer, the thing I need was type=hidden. It is now working on my part. Thanks everyone for helping me. :).
'second.php?text=whatever'? You can't just put whatever to the text, you are doing it wrong. Try this.
firstpage.php
<?php
$whatever = 'Tom & Jerry, 1 + 2 = 3';
echo '' . $whatever . '';
?>
secondpage.php
<form action="thirdpage.php" method="post">
<input type="text" name="username" value="" />
<input type="hidden" name="text" value="<?php echo base64_decode($_GET['text']); ?>" />
<input type="submit" value="Submit" />
</form>
thirdpage.php
<?php
echo 'Username: ' . $_POST['username'];
echo '<br />';
echo 'Text: ' . $_POST['text'];
?>
I have a web site that allows people to upload a csv file and then it loads it into a postgres database. uploading the file is fine and i capture the file name and location ../Data/Uploads/mycsv.csv as $_POST['fname'].
I'm trying to use this variable in $file=file($_POST['fname']) but cant get it to work however if i hard code it in as $file=file("../DATA/Uploads/mycsv.csv") it works. I have attached the code in question. Thanks in advance for any help
Also to clarify echo $_POST['fname']; returns ../DATA/Uploads/mycsv.csv, which is the same as the hard coded value.
please bear with me as im only relatively new to this. I have attached the 2 html forms being used as well. the top one passes the $fname variable containing the file name and path from the php code used to upload the file.
<Form Method="post" Action="../PHP/Loadcsv.php">
<input type="text" value="<?php echo htmlspecialchars($fname);?>" name="fname">
<br />
<Input Type="submit" Value="Continue">
</Form>
this is the php copy the csv into the database
<?PHP
if ($_POST['submit']) {
$file = file(printf($_POST['fname'])); //****doesnt work******
//$file = file("../DATA/Uploads/csv_trial1.csv"); //********This works******
$db = pg_connect("host=localhost dbname=blah user=me password=you");
pg_exec($db, "COPY personaldetails FROM stdin");
foreach ($file as $line) {
$tmp = explode(",", $line);
pg_put_line($db, sprintf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", $tmp[0], $tmp[1], $tmp[2], $tmp[3], $tmp[4], $tmp[5], $tmp[6], $tmp[7]));
}
pg_put_line($db, "\\.\n");
pg_end_copy($db);
?>
below is the html to run the above php.
<form id='form' method='post' action='' >
<input type="submit" name="submit" />
</form>
after running a whole lot of echo to find where the variable is reaching, i dont think it is reaching the inside of the if statement possibly due to the next use of post??
**update**
So after a little playing and bouncing ideas almost literally off my office walls.... i was on the right track and Devon was right too, my problem was the 2 post requests the answer was to have a php variable $filename = $_POST['fname']; to take the variable from the first form and put this into the input for the second form
<form id='form' method='post' action='' >
<input type="hidden" value="<?php echo htmlspecialchars($filename);?>" name="fname">
<input type="submit" name="submit" />
I'm sure there may be other ways to achieve this but at the moment it works.
I'm not sure where you came up with printf(), but any print or echo command will output the arguments to the browser and won't return it to the function at hand. You don't need to use anything special to use a variable as an argument. Just: file($_POST['fname']);
Printf specifically outputs a formatted string and returns the length of the string. So this is the equivalent of calling file(integer) where integer is the length of $_POST['fname']'s value.
I've been trying to create a simple little blog for something I want to add in my school assignment. All I want it to do is to output my input in the order it is entered (like a wall on facebook).
My code is:
<?php
//other form that does the password
$pass = $_POST['pass'];
$blog =$_POST['blog'];
?>
<form method="post"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<textarea placeholder="Write Something, Me."
autofocus
required
autocomplete="off"
name="blog"></textarea>
<br />
<input type="password"
placeholder="Password"
autocomplete="off"
name="pass"
method="post" />
<input type="submit"
name="submit"
value="Submit">
</form>
<?php
If ($pass=="pass") {
echo 'Access Confirmed<br>';
echo $blog;
echo '<br/><br/>';
echo $blog;
echo '<br/><br/>';
echo $blog;
} else {
echo 'Wrong password or invalid blog entry. Try again Noob.<br>';
}
?>
I need to be able to call $blog as an array and output multiples datas as they are being entered (like in facebook). But as you can tell it's just printing the same thing over and over again. Also I don't want it do delete all the inputs if the "Password" (can't really call it secure) is entered incorrectly. I still want to be able to see the previous inputs.
I've tried many things, but none seem to work for me.
If this is unclear and you still have questions, please ask. Thanks.
I think you're a long ways from where you want to be with this. I'll get you started by saying that if you have an array, you can't simply use echo to print it, you need to enumerate through the array and print out the pieces that you're interested in.
foreach ($blog as $value) {
echo $value;
}
Start here and work your way up: http://www.php.net/manual/en/control-structures.foreach.php
I am currently building a multiple pages form using $_SESSION, everything has been working fine so far. Yet, even though I am able to retrieve all the data at the end of the 6 pages, it seems that it is not possible to go back in the form if the user made a mistake.
Let's say he is at page 5 and he realizes that he needs to go back to page 3 to change something, he won't be able to do so. Firefox gives this error message :
Document Expired
This document is no longer available.
The requested document is not available in Firefox's cache.As a security precaution, Firefox does not automatically re-request sensitive documents.Click Try Again to re-request the document from the website.
This is what my forms look like, this one is Form-1.php :
<form action="Form-2.php" method="post">
<fieldset>
<legend>Information</legend>
<p>
<label for="name">Name</label>
<input id="name" type="text" name="name" required/>
</p>
<p>
<label for="age">Age</label>
<input id="age" type="number" name="age" placeholder="Ex. 1988" required/>
</p>
<p>
<input id="sex-m" type="radio" name="sex" value="Male" required/>
<label for="sex-m">Male</label>
<input id="sex-f" type="radio" name="sex" value="Female" required/>
<label for="sex-f">Female</label>
</p>
</fieldset>
<input type="submit" value="Next page"/>
</form>
I am using SESSION to retrieve data from the previous form, this one is from Form-2.php :
session_start();
$_SESSION['name'] = $_POST['name'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['sex'] = $_POST['sex'];
And so on for the six pages.
It works fine, it retrieves all the values correctly, but it is not possible to go back when filling the form.
What am I doing wrong? How can I make sure the user will be able to go back if he makes a mistake?
Thanks for your help.
EDIT :
I ended up with the following code, solving half of my problem. Still from Form-2.php :
session_start();
if (isset($_SESSION['location']) && isset($_SESSION['job'])) {
$location = $_SESSION['location'];
$job = $_SESSION['job'];
} else {
$_SESSION['name'] = $_POST['name'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['sex'] = $_POST['sex'];
$location = '';
$job = '';
}
Then I simply display the values in the fields with :
If text : <?php echo $location; ?>
If radio : <?php if(isset($job) && $job == "Yes"){echo "checked=\"checked\" ";} ?>
It works quite well, the different forms - if the values have been set - are pre-populated just fine.
My problem is, as you can see, if I go back and change a value, it won't be taken into consideration as I am ignoring POST if there is a corresponding value stored in SESSION.
I can't figure out how to make this work properly... Any hint?
Not the best solution, but does the job: Do not use back buttons, provide your own forward, backwards, "step" buttons. Then populate the form with the data stored in e.g. the session.
djot is right, you need a << back >> and << forward >> button, what you can do in functionality is if back button is clicked pass the values of the page/form as hidden input fields and populate the fields using the values in the hidden..