PHP TextBox Value Before Submission - php

I'm building a registration form in Concrete5 but beforehand I need to retreive the User Attributes.
I'm using hidden input to get the values as so:
<input type="hidden" name="akID[<?php echo UserAttributeKey::getByHandle('school')->getAttributeKeyID(); ?>][value]" value='<?php echo $_POST['full_name']; ?>'/>
However I can only get the value if I make a mistake beforehand, i.e. incorrect confirm password and then resubmit the form.
Any ideas on how to get the values without doing this?
Kind Regards

Your example is a bit confusing.
First let me explain why it doesn't work the way you want it to.
You are giving your hidden field a value from $_POST. $_POST only exists once you actually post the form. So on first load of the page, $_POST doesn't exist so $_POST['full_name'] is null.
When you submit the form and there is an error however, the same page reloads but this time $_POST exists since the page reloads after submitting the form.
Here's what is confusing. If on reload $_POST['full_name'] has a value it means you already have a 'full_name' field probably as a text input box. Why then do you want to have a hidden field with the exact same value?
If what you want in this hidden field is the value of a user attribute you need to do 2 things:
1- make sure the user is logged in, else no attributes are available
2- get the user info object to get the attribute value from like so:
$u = new User();
$ui = UserInfo::getByID($u->getUserID());
2- modify the value of the hidden field like so:
value="<?php echo $ui->getAttribute('attribute_handle'); ?>"

Related

Override checkbox value set by PHP

The form used to add a new item into the database and edit existing items is the same form. A "Mode" is passed into the form to tell it if were adding something new or to load the existing item for editing. So....
<input type="checkbox" name="fflreq" id="fflreq" value="<?=$row['FFLr']?>" <?php if ($row['FFLr']=="Yes") {echo 'checked';} ?>>
When a new item is being added, $row['FFLr'] doesn't exist so of course the value is BLANK or NULL or i guess 0 if i don't initially check the checkbox- The form processor coverts this into a "No" and inserts it into the database.
Now here is my problem - When I come back to a item and the form is in edit mode, the VALUE in this checkbox is now "No" - when I am clicking the checkbox to change its status, I see the checkbox become 'checked' but the value is not changing. in other words the click/check status is not setting the value of $_POST['fflreq'] to YES or 1.
I thought, that checking or unchecking a form checkbox replaces whatever is currently in the value='' attribute with a 1 or 0 to represent yes/no on/off or whatever. Why would the value pulled in from the database not change on form submission?
You need to do it in this way:
<input type="checkbox" name="fflreq" id="fflreq" value="Yes" <?php if ($row['FFLr']=="Yes") {echo 'checked';} ?>>
and when submit the form if the above checkbox is checked then you recieved the $_POST["fflreq"] in the form submit page and if it is not checked you recieve nothing in $_POST
so in the submit page you can do this:
$fflreq = "No"
if(isset($_POST["fflreq"]) && $_POST["fflreq"] == "Yes")
{
$fflreq = $_POST["fflreq"];
}
//then you can simply do anything with the $fflreq such as inserting it into database etc.
I hope this can be of some help.
That's not how it works. If you have "checked" the check box then it (along with it's value) will be sent with the post/get (i.e. submission) of the form. If you haven't checked it, then it won't be set...
If the checkbox is active, the browser sends the key/value pair defined in the input tag. However, if the checkbox is not active, nothing at all is sent for this checkbox.
There are two options to deal with this:
The clean option is to be aware of this on the server side, and assume that the checkbox was not active whenever no value comes through.
A more dirty variant is having a <input type="hidden"> tag just before the checkbox, using the same name, but the value you need to see when the checkbox is inactive. This way, when the checkbox is active, you'll still get the desired value from the checkbox, because it will overwrite the hidden value. However, if the checkbox is inactive, you'll get the value from the hidden field.
Not really, the check/unchecked status is read out by looking if the HTML name attribute value is present in the $_POST param.
You can check this with:
<?
if (!empty($_POST['fflreq'])){ /*checked*/ }
else{ /*unchecked*/ }
?>
The value of the HTML attribute value always stays whatever it is in your HTML. So no user interaction (except JS) can change that.
Working with PHP empty() function lets you bypass all the "Yes" "1" string int casting issues.
Further I would use ternary notation for these kind of things:
<input type="checkbox" name="fflreq" id="fflreq"
value="<?=$row['FFLr']?>" <?=(!empty($row['FFLr'])?'checked':'')?>>

Calculating values in PHP with buttons

I am creating a calculator in PHP and I should have this kind of structure of my HTML, where my numbers are clickable buttons, and the number that has been first clicked will be the first value and the second number I click will be the second value. I have no idea on how its gonna work, is it possible? if so how here's my code
<html>
<head>
<title>Calculator</title>
</head>
<body>
<div id = "structure">
<?php
if(isset($_GET)){
$value += $_GET;
}
?>
<form method = "GET " action = "calculator.php">
<input type = "submit" name = "one" value = 1>
<input type = "submit" name = "two" value = 2>
<input type = "submit" name = "three" value = 3>
<input type = "submit" name = "operand" value = "+">
</br>
<input type = "submit" name = "four" value = 4>
<input type = "submit" name = "five" value = 5>
<input type = "submit" name = "six" value = 6>
<input type = "submit" name = "operand" value = "-">
</br>
</form>
</div>
</body>
</html>
I haven't put all the numbers in the calculator for posting purposes
As others mentioned, the best way to do this is to use Client-side coding (Javascript). However, if you have to write a PHP code, then you need to submit the form twice. The first time, put the input variable (first number) in a hidden input element of the form and have print the html again this time with the extra hidden input element. The second time, look for that hidden input value: if it exists then it means that the new input is the second number and the hidden input is the first number. Get the value of the hidden input, get the new number, add them together and display the results. Make sure you reset the hidden input value.
Since this is a book exercise and the point is for you to write the code, I'm not going to give you any code. But, I can point you in the right direction (or at least one of them).
Consider using some kind of value that can be sent with each request that stores the current accumulated value of all operations. This probably should be displayed somewhere and also placed in a field that PHP will be able to read on form submit (Hint: have you ever heard of the hidden input type?). How will you keep track of the last operation? Maybe you could use another one of these fields to store the operation until another button is pushed.
What code can you use to check if the form is submitted? How can you check which button is pushed. Which values will be set if + is pushed? How about if a number is pushed? What if multiple numbers are pushed in a row?
More things to consider: Try playing with a real calculator. When are results displayed? What order of operations is used? Every operation has a left hand side and a right hand side. What happens when you push multiple numbers in a row. What about decimals? Should there be a clear button?
You'll need a way to keep track of previous inputs. Every time you click a button, a request gets sent to the server with that info (in $_GET) but the old inputs are no longer available. A few methods:
Hidden form elements
Each time the user clicks, you read the value and output it back into the page in a hidden form element. Each time you have to read the value of the hidden element using your existing method with $_GET, figure out what you need to do mathematically, and save the old stuff and new stuff into the hidden element again for the next input.
See this page for an example.
Cookies
You can use setcookie to set a cookie with the value the user entered. Each time the user clicks a button, you want to load the value from the cookie and append the current input. When the user clicks the = button, you load the cookie data and parse the expression. You make it easier and store the parts of the expression in some kind of delimited string in the cookie so you have operand,operator,operand and then you can easily compute the result. Hitting a clear button would clear the value of the cookie.
This tizag tutorial is a pretty good example of what you want to do with cookies.
Javascript
Obviously the easiest way. Since the state will be maintained, you don't need to pass values back and forth.

PHP - How can I load the same form with some differences concerning the type of fields?

I have a form with some fields of type text and 2 fields (name and second name) that have a value by default, and these 2 fields are of type="hidden".
Now, I would like to add a link on the form "Fill an order for another person". If the user click on this link the same form must be reloaded but with a little difference: the fields name and second name must be of type text to allow the user enter a name/second name.
I can reload the form, but the fields name and second name remain hidden, because I don't know how define a condition that say to change the types if the user clicks on the link. Could you explain how can I do this?
The fields that are filled by default:
if ($field_label=="Name")
{
return sprintf("<div class='ginput_container'>$name<input name='input_%d' id='$field_id' type='hidden' value='$name' class='ginput_container' $max_length $tabindex $html5_attributes %s/></div>", $id, $field_id, $html_input_type, esc_attr($value), esc_attr($class), $disabled_text);
}
else if ($field_label=="Second name")
{
return sprintf("<div class='ginput_container'>$secondname<input name='input_%d' id='$field_id' type='hidden' value='$secondname' class='ginput_container' $max_length $tabindex $html5_attributes %s/></div>", $id, $field_id, $html_input_type, esc_attr($value), esc_attr($class), $disabled_text);
}
add another input field and call it something like "form_submitted". set the value of this to 1.
you will be able to check for this in your php script once you've submitted the form. if this value is set use text if not use hidden
EDIT: If you want to only display one additional form you can create it on page load and use javascript to make it visible once a user clicks on the link
if you want to be able to add multiple forms, use javascript to build the new ones. each click on the link can add another form. the only thing you need to make sure is that you update the name of the form elements.
You do not need to reload the form. You can just use jQuery to remove the existing hidden field, and create a new textbox in it's place, with the same value as the hidden one.
For example: http://jsfiddle.net/FT2B3/1/

form validation without reset

Is there a way to check the data sent by a form to a PHP page return to the form page WITHOUT resetting the data sent and show a error?
The form has 20 fields and I need to check one of them on a bd. If it fails the user may be redirected to the form page with the form populated and displaying a error message on the field which is 'wrong'.
I would like any advice of a technique instead of populating each field using PHP.
UPDATE:
I do not want to use any solution that involves repopulate the fields by myself!!!
I want a solution that return to the form page populated with the previous values. I've tried something like js.history.back or window.back(). But the form returns empty...
UPDATE: If you are looking for this type of behavior, nowadays there are several different techniques to achive this. I'm currently using jQuery (Ajax).
In your form fields HTML, add posted values as field value.
e.g:
<input type='text' name='email' value='<?php echo $_POST['email']; ?>' />
Yeah, just check the forms beforehand, make sure you run the form HTML after the validation procedure, and if validation fails, reload the form with all of the prior information in the fields.
It might look something like this:
<?php if(isset($_POST['submit']){
// validation code goes here with a trigger variable to
// contain true or false depending on the outcome
}
if($trigger==false){
// load up your post data here
}
echo '
<!-- form HTML goes here -->
';
On your Form Validation page
<?php
$form_values = $_POST;
//Your Form Validation
//$form_validated = TRUE/FALSE; // FALSE if there were errors
if ($form_validated) {$form_values = array();}
$_SESSION['form_values'] = $form_values; /* if your form validation page and form page is not on the same page and can also use the same technique for error messages */
?>
On your Form page for each input field.
<?php
$form_values = $_SESSION['form_values']; //if your form validation page and form page is not on the same page
unset($_SESSION['form_values']);
echo '<input type="text" name="user_name" value="'.$form_values['user_name'].'" />';
?>
Different input types will need to handle the $form_values array differently but this should get your started.
If you are not using a seperate form validation page, you can get the form values directly form the $_POST array.

replace input field with data from mysql php

I created this layout of successive text input fields,
1- Enter data into empty fields
2- Click on button which submits to a php page that updates into database
Now the problem is that i want when i return to the main page again the empty field is replaced with data just added but there are still other empty fields to enter new data.
How can i establish that?
Thanks in advance.
You haven't given a lot of detail but here goes!
You could build your inputs like this:
<input type="text" name="age" value="<?php echo $age; ?>">
When the form first loads, it won't have values for variables like $age, so the input will appear empty. Have the form submit via POST to the same PHP file, run your validation checks, and if everything passes, insert into to your database. (Is it required that you write to the database at this point, or should it wait until the second section is filled out?)
You'll need to use some kind of conditional statement to display the second part of the form. Depending on how complex this is, or whether users will be returning later, you could:
Read the data back out of the
database to check for completeness,
and then display the second part.
Set a variable to track what stage of the form you're in, and based on that, display different sections to be completed.
If you have a way of tracking what stage of the process you're in, you could do something like this:
$formStage = 2;
function isReadOnly($formStage='')
{
if ($formStage == 2) {echo 'READONLY';}
}
and then in your HTML:
<INPUT NAME="realname" VALUE="Hi There" <?php isReadOnly($formStage)?>>

Categories