I'm working on a website for fun where the user enters something into two <input> elements in a form, presses submit, and then the text is logged to a .txt file, then the user is sent to a page for confirmation.
Is this possible to do with a form, and does it require javascript?
The basic structure of something like this would be:
<form method="post" action="">
<input type="text" />
<input type="text" />
<button type="submit"></button>
</form>
Thats possible without javascript.
You need to give each input a name and replace button with input.
Here is a start:
<?php
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
$txt=$_POST['field1'].' - '.$_POST['field2'];
file_put_contents('test.log',$txt."\n",FILE_APPEND|LOCK_EX); // log to test.log (make sure that file is writable (permissions))
header('Location: http://www.google.com/search?q='.$txt); // google as example (use your confirmation page here)
exit();
}
?>
<form method="post" action="">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" >
</form>
You can use $_POST['field1'] and $_POST['field2'] to get the values and log them.
FILE_APPEND is used to add new content to the end of the file instead of overwriting it.
LOCK_EX is used to aquire a write lock for the file (in case multiple users post at the same time)
Related
So what i want do is, have a form displayed on load where it asks user for their details such as name etc, then once the user clicks submit, i want that information to carry over to the next form, i know i have to hidden fields for that.
I want another form to be displayed after they click submit, this all has to be done using POSTBACK, so pretty much having 2 forms on one php page but only displaying the second one after the first has been submitted.
I know i can do this by creating two different php files and using header but i would like to learn how to do it via postback.
<form name="firstform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?> " onSubmit="return validator();">
<p>Please fill in the following form</p>
<p>Given Name* <input type="text" name="fname" id="fname"/><br/>
Middle Name <input type="text" name="mname"/><br />
Family Name* <input type="text" name="lname" id="lname"/><br />
Chosen Username* <input type="text" name="uname"/>
</p>
<p><input type="submit" value="submit" id="submit"/>
</form>
<form name="secondform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?> " >
Test* <input type = "text" name="test"/>
</form>
You can use jquery and Javascript if this is just a quick form
There is a nice form plugin that allows you to send an HTML form asynchroniously.
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
// Hide all forms
$('form').hide();
// Show the first form
$('form').eq(0).show();
$('form').eq(0).on('submit', function() {
// Submit via ajax
// Unhide second form
$(this).hide();
$('form').eq(1).show();
return false;
});
});
</script>
I would validate that the first forms data is correct, then test to see if the form has been submitted without errors.
if (isset($_POST['submit']) && empty($errors)) {
// Output second form
} else {
// Output first form
}
You would either send the first set of data to the database or you can add them as hidden fields in the second form.
This is assuming you put any generated errors in to an array called $errors.
I have a script that users can submit certain data, and after a submit they can "review" the output and go back to previous page or submit to it again and there is my proble, the submitted page: How can I submit it again to write it to a file?
form.html
<form method="post" action="vaihe2.php">
<input name="laskuttaja" type="text" value="Nimi" size="25">
<input name="submit" type="submit" value="Lähetä" />
</form>
vaihe2.php
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
?>
So how can I post that $data to next page(vaihe3.php) with submit and let the script write it to a file. I know how php write file works but the post to third page is not working.
If you wat to go back, the secret is in the value of the input.
<input name="laskuttaja" type="text" value="<?php echo(isset($_POST['laskuttaja'])?$_POST['laskuttaja']:"Nimi";?>" size="25"/>
To 'save' data to the next page use $_SESSIONs. They're simple to use. Just remember everywhere you use them, you must have session_start(); on LINE 1! Can't stress that enough!
$_SESSION['data']=$data;
on your third page:
echo$_SESSION['data'];
More on sessions here.
In vaihe2.php
<form method="post" action="vaihe3.php">
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
echo "<input name=\"laskuttaja\" type=\"hidden\" value=\"".$laskuttaja."\" size=\"25\">";
?>
<input name="submit" type="submit" value="anything" />
</form>
Here you are passing laskuttaja as hidden field and on post will be available to you in third page.
Now data flow as per your requirement. User fills data in form.html -> reviews on vaihe2 and confirms -> gets written in vaihe3.
Could you post the form conditionally back to itself until validated by checkbox? the action would change to "vaihe3.php" ?
<form method="post" action="<?php if ($_POST["valid"]==1) {echo 'vaihe3.php';} ?>">
<input name="laskuttaja" type="text" value="<?php if ($_POST['laskuttaja']!=='') {echo '$_POST[laskuttaja]'} else {echo 'Nimi';} ?>" size="25">
<?php if (isset ($_POST['laskuttaja') && $_POST['laskuttaja']!=="") {
echo 'Please Confirm your answers: <input name="valid" type="checkbox" value="1" />'; } ?>
<input name="submit" type="submit" value="Lähetä" />
</form>
Otherwise, the mention above about CURL would be another option. Or - since your using PHP anyways, you could write the values of form submission to a session array and make them available to all pages until you empty the array.
Okay, so I programmed this code, referencing various websites. I'm trying to program a signup page for a website. How does the html form connects to the PHP/how do I connect it?
I know that one place I have messed up is the action="" in the form. Different websites have told me to put different things in, from the server name ("localhost"), to "" to the name of the file that the php is in (I want to do it in the same file as the form if that is possible, I tried both that and a separate file). What do I put in there so when submit is clicked, it gives the error messages on the same screen as the form, and when submit is clicked and there are no error messages, it continues? Where do I link the page it continues on to?
Also, tell me if any of my code is deprecated. I've been trying to check everything, but I could of missed something.
<?php
include 'connect.php';
//if submit is clicked
if (isset($_POST['submit'])) {
//then check if all fields are filled
if (!$_POST['username'] | !$_POST['password'] | !$_POST['firstname'] | !$_POST['MI'] | !$_POST['lastname'] | !$_POST['email'] | !$_POST['phonenumber'] | !$_POST['country'] ) {
die('You did not complete all of the required fields'); }
$usernamesquery = mysql_query("SELECT * FROM logins WHERE username='$usernametest'");
if(mysqli_stmt_num_rows($usernamesquery) > 0) {
die('This username is already taken.');
}
}
?>
<form action="????????" method="post">
Username: <input type="text" name="username" maxlength="30"><br>
Password: <input type="password" name="password" maxlength="30"><br>
First Name: <input type="text" name="firstname" maxlength="30"><br>
Middle Initial: <input type="password" name="MI" maxlength="30"><br>
Last Name: <input type="text" name="lastname" maxlength="30"><br>
Email: <input type="password" name="email" maxlength="50"><br>
Phone Number: <input type="text" name="phonenumber" maxlength="11"><br>
Country: <input type="password" name="country" maxlength="40"><br>
<input type="submit">
</form>
If you want it to direct to the same file, then just use:
<form action="" method="POST">
Also there is no $_POST["submit"] since you haven't given your submit button a name.
<input type="submit" name="submit">
Also, does $usernametest actually contain anything? Since you haven't given it a value in your code above.
The "action" is page with some script, that parse the form data and do the login.
For example: the form has action "login.php", that means after you submit the form, the data are sent to "login.php" where you can acces it via $_POST variable. If you have login logic and form in the same file, you don't have to set any action, it's ok if you do it like this
<form action="" method="POST">
More info here
If you want to submit the form to the same page (the URL you are allready on), you can just leave out the action from the <from> tag. Otherwise, you specify the URL (relative or absolute) the form needs to be submitted to.
I want a simple form with one text box and a submit button. For example: If a user enters "foobar" into the text box and hits enter, they should be redirected to mysite.com/browse/foobar
Does anyone know how I can do this in php? thanks
Assuming name='q' for the text input
<?php
if ($_GET['q'] === "foobar") {
header("Location: http://example.com/browse/foobar");
}
?>
The Form:
<form action="index.php" method="get">
<input type="text" name="q" />
<input type="submit" />
</form>
index.php
header("Location: http://example.com/browse/".$_GET['q']);
Note: this is not good practice but it works.
I am trying to run a function from a PHP script in the form action.
My code:
<?php
require_once ( 'username.php' );
echo '
<form name="form1" method="post" action="username()">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
I echo the form but I want the function "username" which is called from username.php to be executed. how can I do this in a simliar way to the above?
<?php
require_once ( 'username.php' );
if (isset($_POST['textfield'])) {
echo username();
return;
}
echo '
<form name="form1" method="post" action="">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
You need to run the function in the page the form is sent to.
In PHP functions will not be evaluated inside strings, because there are different rules for variables.
<?php
function name() {
return 'Mark';
}
echo 'My name is: name()'; // Output: My name is name()
echo 'My name is: '. name(); // Output: My name is Mark
The action parameter to the tag in HTML should not reference the PHP function you want to run. Action should refer to a page on the web server that will process the form input and return new HTML to the user. This can be the same location as the PHP script that outputs the form, or some people prefer to make a separate PHP file to handle actions.
The basic process is the same either way:
Generate HTML form to the user.
User fills in the form, clicks submit.
The form data is sent to the locations defined by action on the server.
The script validates the data and does something with it.
Usually a new HTML page is returned.
A simple example would be:
<?php
// $_POST is a magic PHP variable that will always contain
// any form data that was posted to this page.
// We check here to see if the textfield called 'name' had
// some data entered into it, if so we process it, if not we
// output the form.
if (isset($_POST['name'])) {
print_name($_POST['name']);
}
else {
print_form();
}
// In this function we print the name the user provided.
function print_name($name) {
// $name should be validated and checked here depending on use.
// In this case we just HTML escape it so nothing nasty should
// be able to get through:
echo 'Your name is: '. htmlentities($name);
}
// This function is called when no name was sent to us over HTTP.
function print_form() {
echo '
<form name="form1" method="post" action="">
<p><label><input type="text" name="name" id="textfield"></label></p>
<p><label><input type="submit" name="button" id="button" value="Submit"></label></p>
</form>
';
}
?>
For future information I recommend reading the PHP tutorials: http://php.net/tut.php
There is even a section about Dealing with forms.
I'm not sure I understand what you are trying to achieve as we don't have what username() is supposed to return but you might want to try something like that. I would also recommend you don't echo whole page and rather use something like that, it's much easier to read and maintain:
<?php
require_once ( 'username.php' );
if (isset($_POST)) {
$textfield = $_POST['textfield']; // this will get you what was in the
// textfield if the form was submitted
}
?>
<form name="form1" method="post" action="<?php echo($_SERVER['PHP_SELF']) ?">
<p>Your username is: <?php echo(username()) ?></p>
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>
This will post the results in the same page. So first time you display the page, only the empty form is shown, if you press on submit, the textfield field will be in the $textfield variable and you can display it again as you want.
I don't know if the username() function was supposed to return you the URL of where you should send the results but that's what you'd want in the action attribute of your form. I've put the result down in a sample paragraph so you see how you can display the result. See the "Your username is..." part.
// Edit:
If you want to send the value without leaving the page, you want to use AJAX. Do a search on jQuery on StackOverflow or on Google.
You would probably want to have your function return the username instead of echo it though. But if you absolutely want to echo it from the function, just call it like that <?php username() ?> in your HTML form.
I think you will need to understand the flow of the client-server process of your pages before going further. Let's say that the sample code above is called form.php.
Your form.php is called.
The form.php generates the HTML and is sent to the client's browser.
The client only sees the resulting HTML form.
When the user presses the button, you have two choices: a) let the browser do its usual thing so send the values in the form fields to the page specified in action (in this case, the URL is defined by PHP_SELF which is the current page.php). Or b) use javascript to push this data to a page without refreshing the page, this is commonly called AJAX.
A page, in your case, it could be changed to username.php, will then verify against the database if the username exists and then you want to regenerate HTML that contains the values you need, back to step 1.
I think it should be like this..
<?php
require_once ( 'username.php' );
echo '
<form name="form1" method="post" action="<?php username() ?>">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
It's better something like this...post the data to the self page and maybe do a check on user input.
<?php
require_once ( 'username.php' );
if(isset($_POST)) {
echo "form post"; // ex $_POST['textfield']
}
echo '
<form name="form1" method="post" action="' . $_SERVER['PHP_SELF'] . '">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
Is it really a function call on the action attribute? or it should be on the onSUbmit attribute, because by convention action attribute needs a page/URL.
I think you should use AJAX with that,
There are plenty PHP AJAX Frameworks to choose from
http://www.phplivex.com/example/submitting-html-forms-with-ajax
http://www.xajax-project.org/en/docs-tutorials/learn-xajax-in-10-minutes/
ETC.
Or the classic way
http://www.w3schools.com/php/php_ajax_php.asp
I hope these links help
You can put the username() function in another page, and send the form to that page...