I am using sessions to repopulate values in form,Its look something like this
My form have some fields and I want to populate user entered values after sever side validattions.Below is the form:
<html>
<form>
Name: <input type="text" id="name" name="name" value="<?php if(isset($_SESSION['NAME'])) echo $_SESSION['NAME'];?>" >
Address:<input type="text" id="address" name="address" value=<?php if(isset($_SESSION['ADDRESS'])) echo $_SESSION['ADDRESS']; ?>>
</form>
</html>
I am storing the values in action page like this:
<?php
$_SESSION['NAME'] = $strname ;
$_SESSION['ADDRESS'] = $straddress;
?>
want to know whether this is the right way to do it,to populate user entered values after server side validation.Although it works fine.
There is no need to store it in session variables if you need it to use just after you have posted the form. You can either use $_POST or $_GET depending upon the form submission method you use.
Also you can use extract function on $_POST or $_GET to get the posted values using it's field name.
Related
I have the following code snippet of my fields I have in my form:
<input id="username" type="text" placeholder="E-mail Address" value="" name="username"></input>
This is what I have in my input field. Is there anybody who will tell me how to get input values to the field using a url? e.g https://mysite?username=ken and it will show "ken" in the input field?
In your HTML, add the input field like this:
<input type="text" name="username" value="<?php echo htmlspecialchars($_GET['username']); ?>" />
Basically, the value attribute of the text field needs to be set to:
<?php echo $_GET['username']; ?>
The code right above this is how you would output a get variable in php whether you are putting it in a text field or not.
To access get variables, always use:
$_GET['variable_name'];
Then you can assign it to variables or pass it as a function parameter.
**However, I strongly do not recommend passing sensitive information like usernames and passwords through GET variables. **
First off, users could change the URL hence changing the variable. They could also accidentally share the URL with someone and that could give someone else access to their account. I would recommend that you create a cookie on their machine that is set to a random ID, and then in a MySQL database, associate that ID with a username so that you know the user can't accidentally share their account or change their username through the URL.
You can do it like this, make an isset in your php form input that can catch your ken variable from GET post, never forget the method="get" inside the form tag and if you are planning on submitting on the same page you can use action="<?php echo $_SERVER['PHP_SELF']; ?>" inside your form tag.. hope this helps, here is your code.. ^_^
<form id="form" name="form" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<p>Input</p>
<div>
<input type="text" name="nameoffield" id="nameoffield" value="<?php if(isset($_GET['ken'])){echo $_GET['ken'];} ?>"> <br />
</div>
</fieldset>
<div>
<button type="submit" value="Submit" name="submit">
Submit
</button>
</div>
</form>
The <input> tag and other fields of form must be in a <form>tag.
<form action = "https://mysite" method = "get">
<input id = "username" type = "text" placeholder = "E-mail Address" name = "username" value = "<?php echo $_GET['username']; ?>" />
</form>
In the above code, form tag specifies that the method of submission is 'GET' and the action that will be taken on submission is URL to which your form data will be submitted and processed.
Now assuming that your form is in the same URL to which you are submitting your form, you will get the GET value in the same page (or URL), so in the input text field set the value which is obtained by GET method and use it.
All the GET key-value pairs are stored in an associative array $_GET from which you can access the value of a given key by using that as the index of the array.
e.g. Key is username in this case, so to get the value of the username, $_GET['username'] was used.
There are some inputs, and there is a function. The function requires these inputs, and the inputs are user-given. But, the buttons that fire the function and the input submission form are two different buttons. So, when the user presses "submit" to store his variables, the variables are stored fine. But, when he presses the "calculate" button (which fires the function), php says "undefined index" because it reads the $_POST of that input again and again.
If I disable register_globals, it does not show 'undefined index' but these values are 0 again.
If I use another file just to store these values and then redirect back to the page where the function button is, there is a redirect loop, require_once does not work.
What is the way to store the inputs in such way that they can be used again and again in functions and whatsoever? No databases, I need a way to store them in variables.
edit: the form: <label for="asdf">enter value:</label> <input type="text" id="asdf" name="asdf" value="<?php echo $asdf;?>" />
storing the value:
$asdf=$_POST['asdf'];
then I need to write $asdf in the function with the updated value that the user gave through the html form. How to do it? Cannot be much simpler
I would just store them in the session. That way they, they can be used across php scripts, but are not stored in the long-term. Here's an example:
form.php
<?php
session_start();
?>
<html>
<body>
<form action="store.php">
<input type="text" name="x" value="<?php echo $_SESSION['x'] ?>">
<input type="text" name="y" value="<?php echo $_SESSION['y'] ?>">
<input type="submit" value="Submit">
</form>
<form action="calculate.php">
<input type="submit" value="Submit">
</form>
</body>
</html>
store.php
<?php
// Start the session
session_start();
$_SESSION["x"] = $_POST['x']; // substitute your input here
$_SESSION["y"] = $_POST['y']; // substitute your input here
?>
calculate.php
<?php
// Start the session
session_start();
$result = $_SESSION["x"] * $_SESSION["y"];
echo $result;
?>
There is no way to store them in variables. Every request to your server is a new request.
You could store the variables in a cookie/session or give them back after pushing the first button and store them in a hidden field in your html form. Or store them in a file on your server.
I am making account registeration page with php. My problem is taht, if somebody uses username which is already in use, page reloads and all inputs will empty.
I tried this:
<form action="register.php" method="post">
<input type="text" name="username" value="<?php if(isset($_POST['username'])) { echo $_POST['username']; } ?>" />
</br>
In my opinion, the best way is to send the check for a duplicate username via AJAX. Use JavaScript to send the request a second or two after the user is done typing.
If you don't want to use AJAX, store the field values in the session and spit them back out when the page reloads. This is your page:
<?php if(isset($_SESSION['username'])) { echo $_POST['username']; } ?>
Then, in the form submission code:
$_SESSION['username'] = $_POST['username'];
Your code could work if your form-handling code and you form markup are in the same PHP file, but you certainly aren't violating separation of concerns like that, are you? ;)
You have set the method of your form to POST, so you can't use $_GET, instead you have to use $_POST.
<form action="register.php" method="post">
<input type="text" name="username" value="<?php echo htmlspecialchars($_POST['username']); ?>" />
</br>
I am try to get the value of the input field with a custom attribute I have created using PHP. This is my code:
<form action="uploadform.php" method="post">
<input type="text" name="email" id="email" mynewattribute="myemail">
<input type="submit" name="submit">
</form>
//uploadform.php
<?php
//I know $name = $_POST['email']; will give me the value but I would like to get the value of the input field with "mynewattribute" and not name. Is it possible?
?>
The web browser doesn't know what to do with your custom attribute, so will simply ignore it. The only data sent when you submit the form is the values of "successful" elements. So your custom data will never be sent, and can never be read by the receiving script.
The best place to put such data is into hidden input fields. One possibility is to use names with square brackets in, which PHP automatically converts into arrays. e.g.
<input type="text" name="email[value]">
<input type="hidden" name="email[magic]" value="true">
Populates an array like this:
$_POST['email']['value'] = '';
$_POST['email']['magic'] = 'true';
I have a basic contact form on my website and I am trying to add the PHP ucwords() function of PHP to the form for the users first_name and last_name fields so they capitalize the first letter correctly. How would I add this to the actual HTML form?
Edit: I want these changes to be applied only after the user submits the form. I don't really care about how the user types it in. I just need someone to actually show me an example.
Like how would I add the PHP ucwords() code to this simple form?
<!DOCTYPE html>
<html>
<body>
<form action="www.mysite.com" method="post">
First name: <input type="text" name="first_name" value="" /><br />
Last name: <input type="text" name="last_name" value="" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
I am assuming I do something like value='<php echo ucwords() ?>' but I have no idea how?
Thanks!
When user submit the form you can access the submitted information through $_POST variable [because method="post"] of PHP and in action you have to specify the actual page where you need the submitted information to be process further
<?php
// for example action="signup_process.php" and method="post"
// and input fields submitted are "first_name", "last_name"
// then u can access information like this on page "signup_process.php"
// ucwords() is used to capitalize the first letter
// of each submit input field information
$first_name = ucwords($_POST["first_name"]);
$last_name = ucwords($_POST["last_name"]);
?>
PHP Tutorials
Assuming short tags are enabled:
$firstName = 'Text to go into the form';
<input type="text" name="first_name" value="<?=ucwords($firstName)?>" />
Otherwise as you stated
<input type="text" name="first_name" value="<?php echo ucwords($firstName); ?>" />
Assuming you wanted to do it without a page refresh, you need to use Javascript. Simplest way would be to add an onkeyup event to the input field and simulate PHP's ucwords functions, which would look something like...
function ucwords(str) {
return (str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
return $1.toUpperCase();
});
}
Edit: In response to your edit, if you want to get the value they sent with ucwords applied, all you need to do is $newVal = ucwords($_POST['fieldName']);