Fill HTML-textfield-value with PHP $_GET-parameter - php

I have a link like this:
/index.php?textfieldvalue=test123
This link navigates to a site with a textfield. If this site is opened, the default-value should be "test123". To do this i used the PHP $_GET-parameter "textfieldvalue".
My textfield looks like this:
<input type="text" value="<?php $_GET('textfieldvalue') ?>" required />
But the textfield is still empty....
(And $_GET('target') is not empty! I printed it for testing seperate out)

Please remove '&' After '?' and in input field write:
<input type="text" value="<?php echo $_GET['textfieldvalue'] ?>" required />

Related

Access form input in the same form

I am taking input from user in a form. Can I get that value(user input) to calculate other fields in the same form.
<input size="12" id="inputField" name="inputField" autofocus="" type="date" onblur="return dateValidate(this)"/>
Can I collect this form input later in the form and use it to calculate other fields. I was trying to using to use $_POST to retrieve the value but I am not sure if thuis is the right thing.
You cannot do it in PHP before submitting the form. You can easily do so in JavaScript. However you could simply add some AJAX code to send the value to your PHP script when user enters something in the box, and parse the response accordingly. PHP works on server side, and does not interact with user without any server side request
You can certainly do it in 2 ways...
1st way
<?php
$first_digit = '';
$second_digit = '';
$third_digit = '';
if(isset($_POST['calculate'])) {
$first_digit = $_POST['first_digit'];
$second_digit = $_POST['second_digit'];
$third_digit = $first_digit + $second_digit;
}
?>
<form method="POST">
<input type="text" name="first_digit" value="<?php if(isset($first_digit)) echo $first_digit; ?>" />
<input type="text" name="second_digit" value="<?php if(isset($second_digit)) echo $second_digit; ?>" />
<input type="text" name="third_digit" readonly value="<?php if(isset($third_digit)) echo $third_digit; ?>" />
<input type="submit" name="calculate" value="Calculate" />
</form>
2nd Way
Total the variables in the code and instead of showing the result output in a text box you can instead calculate and echo out the result, or you can store that in the database.
Note: Be sure you keep the third input[type=text] as readonly as it is showing you the calculated value so probably you don't want your users to change
Yes you can get input from user to calculate other fields in the same form, you should use scripts like OnChange() or onClick() to have this.

Check type of an input field with php

I would like to check the type of an input field text is. Cause i only want to make thos fields read only. I know i can do this in javascript, but is there any way that this is possible in php?
best regrads.
same as javascript use this
<input name="address" type="text" readonly="readonly">
this make field read only you can't change it.
<input name="address" type="text" value="<?php echo "your value"; ?>" readonly="readonly">
try this code
To disable an input field add disabled attribute:
disabled="disabled"
This has to be done when generating the html or with javascript after generation.
This is a clean way of doing it and the user sees directly that it is just readable but not editable.
When you submit a form, in php you take the name attribute to get the posted value. Say you have
<input name="address" type="text">
In php:
if($_POST['address']){
//don't process address cause you know this is of type text
}
This is how you can do it in php,if $field is false then set input disabled
<input type="text" name="" <?php echo ($field == false)? 'disabled="disabled"' : '';?> value="" />

Joomla Extra User Field

I'm working with Joomla User Form. I need to create some extra fields. I googled, and got this tutorial
I followed but that wasn't enough for me because I have to handle with checkbox and radio button. The following form code didn't save submission data (I mean checked/uncheked).
<input class="inputbox" type="checkbox" name="hasweb" id="hasweb" size="40" value="<?php echo $this->user->get('hasweb');?>" />
But the following is fine when I put any data there.
<input class="inputbox" type="text" name="hasweb" id="hasweb" size="40" value="<?php echo $this->user->get('hasweb');?>" />
I'm novice in Joomla. Please help me.
There is a difference between the value and the checked state with HTML checkbox elements. So if you want to have the checkbox represent the actual choice you have to do something like this:
<input class="inputbox" type="checkbox" name="hasweb" id="hasweb" size="40" value="reallyhasweb" <?php echo $this->user->get('hasweb') == "reallyhasweb" ? 'checked="checked"' : ''; ?>" />
This should place checked="checked" in the HTML when the user has selected the checkbox and will make the checkbox selected.

How do I retrieve a PHP variable in my HTML code?

I call a PHP script from my HTML form submit.
Then I process the values retrieved from this HTML form in the PHP script and now I want to display these values in another HTML form that I am calling from this PHP script.
How do I retrieve this variable?
Note: I tried echo but I think I actually want to display this value in the form (HTML) and not break it again in a PHP tag.
I'm not sure what you mean by "not breaking it again with a PHP tag". HTML on its own cannot access PHP variables. This is because PHP is a server-side language and HTML is a client side language. But here is the simplest way to print php variables retrieved from a form.
<form>
<p>Your name: <?php echo $_POST['name'] ?></p>
</form>
This is assuming that there was a form that submitted a field called 'name' using POST.
You can process the name in a php script at the top of the file and then simply echo it when you're printing the html. This way, you won't have too much php code mixed in with the HTML (which makes it look cleaner).
Once you got the values in the PHP script, are you calling a new script? If so, you might wanna save the values in $_SESSION["varible_name"]. If not, you just have to echo it.
It depends on how you are accessing your form data, either through $_POST or through $_GET. For simplicity, I'll assume your using $_GET and modify this example for more clarity.
So lets say you have a form hosted on welcome.php:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
Now the results will be returned back to the same page, so you want to modify the page to say:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" value="<?php echo $_GET["fname"]; ?>"/>
Age: <input type="text" name="age" value="<?php echo $_GET["age"]; ?>" />
<input type="submit" />
</form>
Though you'll notice that we're using the same page, and we can only have one version of the page, so we want to render if upon the condition that our variable has been set.
if (isset($_GET["fname"])){
//code to print second form
}
else{
//code to print first form
}
Or, in another way (using the ternary operator):
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" value="<?php echo ((isset($_GET["fname"]))?$_GET["fname"]:""); ?>"/>
Age: <input type="text" name="age" value="<?php echo ((isset($_GET["age"]))?$_GET["age"]:""); ?>" />
<input type="submit" />
</form>

How can I set the value of a textbox through PHP?

So I have this empty textboxes in a registrationg page. The user enters some data, hits continue and then there's a confirmation page. If the data is incorrect, the user hits go back to go correct whatever was wrong. However, when he goes back, all the textboxes are empty. So the first thing that comes to my mind is to store the user data in a Session (I have a User class that holds all this data so I store the class in the session). When the user goes back I am able to retrieve the data.
I do something like this:
if($_SESSION['UserInfo'])
{
$user = $_SESSION['UserInfo'];
$firstName = $user->FirstName;
$lastName = $user->LastName;
}
How would I put these variables in a textbox?
To set the value, you can just echo out the content inside the value attribute:
<input type="text" name="firstname" value="<?php echo htmlentities($firstName); ?>" />
<input type="text" name="lastname" value="<?php echo htmlentities($lastName); ?>" />
Of course you will want to escape it but...
<input type="text" value="<?php echo $firstName ?>" />
or if the form is posted, it would be easier to do:
<input type="text" name="firstName" value="<?php echo $_POST['firstName'] ?>" />
fine... even though it was out of the scope of the question here is the escaped version:
<input type="text" name="firstName" value="<?php echo htmlentities($_POST['firstName']) ?>" />
smth like
<input type="text" value="<?php echo $first_name;?>">
Don't forget to escape with htmlentities() or smth similar. If you don't know why - google XSS.

Categories