Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
I need someone to help me with php guides on how i can echo every user input to the screen...
Example:
Html
<form action"bot.php" method="POST">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
Php Code
<?php
//request name from form
$username = $_REQUEST['username'];
//create function
function msg()
{
$username = $_POST['username'];
echo "$username";
echo "\r\n";
}
//echo input to screen
if (isset($_POST['submit']))
{
msg();
}
If I click submit button it will display my input text to the screen, that's fine, If I input another username click on submit button again to display under my previous output to screen...Instead PHP overide my previous output with new username submitted..
Will be glad if anyone can help
This example uses a session. You don't need anything external other than a working PHP installation. I've added comments in the code, but the basic flow is,
start a session
read the names from the session. If this is a new session, default the names to an empty array
read the submitted name
append the submitted name to the existing names
write the existing names to the session
<form action="bot.php" method="POST">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
<?php
// Always start the session
session_start();
// Check if the form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Read the existing names from the session. Default to an empty array if none are set
$existingNames = $_SESSION['names'] ?? [];
// Grab the submitted name
$name = $_POST['name'];
// Add it to the end of the list
$existingNames[] = $name;
// Overwrite the entire session's list
$_SESSION['names'] = $existingNames;
// Output something interesting
echo 'Welcome ' . $name;
echo '<br>';
echo 'There are ' . count($existingNames) . 'name(s).';
echo '<br>';
foreach ($existingNames as $storedName) {
echo $storedName;
echo '<br>';
}
}
Related
First of all I'll be sincere, I'm a student and I've been asked to do a task that seems impossible to me. I don't like asking questions because generally speaking I've always been able to fix my coding issues just by searching and learning, but this is the first time I've ever been on this possition.
I need to create a php file that contains a form with two inputs that the user fills. Once he clicks submit the website will show on top of it the two values. Till here I haven't had an issue, but here's the problem, the next time the user sends another submission, instead of clearing the last 2 values and showing 2 new ones, now there needs to be 4 values showing.
I know this is possible to do through JSON, the use of sessions, Ajax, hidden inputs or using another file (this last one is what I would decide to use if I could), but the teacher says we gotta do it on the same html file without the use of any of the methods listed earlier. He says it can be done through an Array that stores the data, but as I'll show in my example, when I do that the moment the user clicks submit the array values are erased and created from zero. I know the most logical thing to do is asking him, but I've already done that 4 times and he literally refuses to help me, so I really don't know what to do, other than asking here. I should point out that the answer has to be server side, because the subject is "Server-Side Programming".
Thank you for your help and sorry beforehand because I'm sure this will end up being a stupid question that can be easily answered.
For the sake of simplicity I erased everything that has to do with formatting. This is the code:
<?php
if (isset($_POST['activity']) && isset($_POST['time'])){
$agenda = array();
$activity = $_POST['activity'];
$time = $_POST['time'];
$text = $activity." ".$time;
array_push($agenda, $text);
foreach ($agenda as $arrayData){
print implode('", "', $agenda);
}
}
?>
<html>
<head>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<label for="Activity">Activity</label><br>
<input name= "activity" type="text"><br><br>
<label for="Time">Time</label><br>
<input name= "time" type="time"><br><br>
<input type="submit">
</form>
</body>
</html>
Your question was not very clear to be honest but I might have gotten something going for you.
<?php
$formaction = $_SERVER['PHP_SELF'];
if (isset($_POST['activity']) && isset($_POST['time'])){
$agenda = array();
//if the parameter was passed in the action url
if(isset($_GET['agenda'])) {
$agenda = explode(", ", $_GET['agenda']);
}
//set activity time
$text = $_POST['activity']." ".$_POST['time'];
//push into existing array the new values
array_push($agenda, $text);
//print everything
print implode(", ", $agenda);
//update the form action variable
$formaction = $_SERVER['PHP_SELF'] . "?agenda=" . implode(", ", $agenda);
}
?>
<html>
<head>
</head>
<body>
<form action="<?php echo $formaction; ?>" method="POST">
<label for="Activity">Activity</label><br>
<input name= "activity" type="text"><br><br>
<label for="Time">Time</label><br>
<input name= "time" type="time"><br><br>
<input type="submit">
</form>
</body>
</html>
SUMMARY
Since you cant save the posted values into SESSION vars or HIDDEN input, the next best thing would be to append the previous results of the posted form into the form's action url.
When the form is posted, we verify if the query string agenda exists, if it does we explode it into an array called $agenda. We then concatenate the $_POST['activity'] and $_POST['time'] values and push it to the $agenda array. We then PRINT the array $agenda and update the $formaction variable to contain the new values that were added to the array.
In the HTML section we then set the <form action="" to be <form action="<?php echo $formaction; ?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm working on a small assignment and ran into an issue I can't seem to figure out. The assignment consists of three pages. The first page consists of a simple field that accepts user input. The second page validates that input, and if valid, displays a second input field. The third page validates page 2 input, then adds it to page 1 input.
I've used a hidden field to store page 1 input on page 2, in order to submit it to page 3. But, because page 2 has to display the form only if the previous input is valid, I implemented the form inside a php conditional statement. So, I directly referenced the php variable holding page 1 input as the hidden field value. Here is my page 2 code:
<?php
if (isset($_POST["get_number1"] )) {
$number1 = $_POST['get_number1'];
$button_pressed = $_POST['sbmt'];
$message;
$error1 = false;
if ($number1==null) {
$message = 'ERROR: input field empty';
$error1=true;
}
else if (!is_numeric($number1)) {
$message = 'ERROR: input must be numeric';
$error1=true;
}
else if (strpos((String)$number1,'.')!=null && strlen((String)$number1)-strpos((String)$number1,'.')>4) {
$message = 'ERROR: input must contain 0 to 3 decimals';
$error1=true;
}
else if (!strcmp($button_pressed,"sbmt")){
$message = 'ERROR: submit button not pressed';
$error1 = true;
}
}
?>
<html>
<head>
<title>GetNumber2.html</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<?php
if ($error1==true){
echo $message .'<br>'. 'Return to Form 1';
}
else {
echo 'Your number 1: '.$number1.
' <form action="AddNumbers.php" method="post">
<label>Enter the second number (format : 999999.999) :</label>
<input type="text" name="get_number2">
<input type="submit" name="sbmt" value="SubmitNumber">
<input type="hidden" name="hf_number1" value="$number1";>
</form>
';
}
?>
</body>
</html>
The next page displays the hidden field value as $number1, even if I remove the quotation marks. Any hints would be appreciated. Thank you.
Strings with ' (single quotes) are not parsed.
This means that any variable in ' will be considered a string.
Use concatenation, for example:
$str = '<input type="hidden" name="hf_number1" value="' . $number1 . '">';
This question already exists:
I need help passing variables from html to php [closed]
Closed 7 years ago.
I am trying to make a chatroom in html and php i have it working but it can only post to one page and i want it to be able to post to different pages that the user chooses by typing the page name into a textbox in a html form
and puting there name position and message here is the code i have so far
<?php
if($_POST) {
$Chatroom_name = $_POST['Chatroom'];
$position = $_POST['position'];
$name = $_POST['name'];
$content = $_POST['commentContent']
want variable here--->$handle = fopen('$Chatroom_name',"a");
fwrite($handle,"<b>" . $name . "</b>:<br/>" . $position . "</b>:<br/>" . $content . "<br/><br><br>");
fclose($handle);
}
?>
<html>
<head>
</head>
<body>
<br><br><br>
<form name="chatroomname" action = "" method = "POST">
Chatroom Name: <br>
<input type = "text" name = "Chatroom"><br>
Name:<br>
<input type = "text" name = "name"><br/>
Position:<br>
<input type = "text" name = "position"><br/>
Message:<br>
<textarea rows = "10" cols = "30" name = "commentContent"></textarea><br/>
<input type = "submit" value = "Send"><br/>
</form>
</body>
</html>
can someone please help me it is for a big project i am doing
any help is greatly apriciated :)
You should use sessions in php.
session_start();
if(isset($_POST['submit'])){
$_SESSION['name'] = $_POST['name'];
}
In further pages, just make sure you start the session, and then echo the variable.
session_start();
echo $_SESSION['name']; //this will print the name submitted
You should set your form's action to point to the name of this file. That way when you submit the form it'll reload the page and send the POST data along with it.
You can output the variable's content into the form by doing something like this:
<input type = "text" name = "Chatroom" value="<?php if(isset($Chatroom_name)){ echo $Chatroom_name; }">
i want to make a add cricket stats page (witch i have done) but when i fill in the form and press submit i made it say echo "$name stats have been added"; but when i add a new persons stats that dispersers and is replaced by a different the one i just made, how can i make it stay every time i add a new one so i can see who's stats i have added?
Create an array of names stored in $_SESSION and keep adding to it on each post. Then display them all each time.
session_start();
// Initialize the array
if (!isset($_SESSION['names'])) {
$_SESSION['names'] = array();
}
// Add the newest name to the array
$_SESSION['names'][] = $name;
// Display them all in a loop with linebreaks
foreach ($_SESSION['names'] as $cur_name) {
echo "$cur_name stats have been added<br />\n";
}
EDIT:
To reset them, pass ?action=reset in the URL querystring as www.example.com?action=reset
<form action='scriptname.php' method="get">
<input type="hidden" name="action" value="reset" />
<input type="submit" value="Reset list" />
</form>
// Remove the session array on reset.
if (isset($_GET['action']) && $_GET['action'] == "reset")
{
unset($_SESSION['names']);
}
why dont you just insert the new name you wanna add, into your DB ? and make a select when you wanna view some names
I'm new to forms and post data ... so I don't know how solve this problem!
I've a php page (page1) with a simple form:
<form method="post" action="/page2.php">
<input type="search" value="E-Mail Address" size="30" name="email" />
<input type="submit" value="Find E-Mail" />
</form>
How you can notice ... this form post the 'email' value to the page2. In the page2 there is a small script that lookup in a database to check if the email address exist.
$email = $_POST['email'];
$resut = mysql_query("SELECT * FROM table WHERE email = $email");
.
.
.
/* do something */
.
.
.
if($result){
//post back yes
}
else{
//post back no
}
I don't know how make the post back in php! And how can I do to the post back data are read from a javascript method that shows an alert reporting the result of the search?
This is only an example of what I'm trying to do, because my page2 make some other actions before the post back.
When I click on the submit button, I'm trying to animate a spinning indicator ... this is the reason that I need to post back to a javascript method! Because the javascript function should stop the animation and pop up the alert with the result of the search!
Very thanks in advance!
I suggest you read up on AJAX.
Here's a PHP example on W3Schools that details an AJAX hit.
Hi i think you can handle it in two ways.
First one is to submit the form, save the data in your session, check the email, redirect
back to your form and display the results and data from session.
Like
session_start();
// store email in session to show it on form after validation
$_SESSION['email'] = $_POST['email'];
// put your result in your session
if ($results) {
$_SESSION['result'] = 'fine';
header(Location: 'yourform.php'); // redirect to your form
}
Now put some php code in your form:
<?php
session_start();
// check if result is fine, if yes do something..
if ($_SESSION['result'] == 'fine) {
echo 'Email is fine..';
} else {
echo 'Wrong Email..';
}
?>
More infos : Sessions & Forms
And in put the email value back in the form field
<input type="search"
value="<?php echo $_SESSION['email']; ?>"
size="30"
name="email" />
Please excuse my english, it is horrible i know ;)
And the other one the ajax thing some answers before mine !
As a sidenote, you definitly should escape your data before using it in an SQL request, to avoid SQL injection
As you are using mysql_* functions, this would be done with one of those :
mysql_escape_string
or mysql_real_escape_string
You would not be able to post in this situation as it is from the server to the client. For more information about POST have a look at this article.
To answer your question you would want to do something like this when you have done your query:
if(mysql_num_rows($result)){ //implies not 0
$data = mysql_fetch_array($result);
print_r($data);
}
else{
//no results found
echo "no results were found";
}
The print_r function is simply printing all the results that the query would have returned, you will probably want to format this using some html. $data is just an array which you can print a single element from like this:
echo $data['email'];
I hope this helps!
<?php
echo " alert('Record Inserted ');"
OR
echo " document.getElementByID('tagname').innerHtml=$result;"
?>
OR
include 'Your Html file name'