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.
Related
I have a form located at a url containing get parameters,my form is also using this method.When the form is submitted it rewrites the previos get parameters.
Is there a simple way to rewrite only my form parameters?
I have in mind a Javascript solution ,however I want to know if there is a simpler way?Using HTML/PHP perhaps?
As far as I know, u u are not interested in using JS, then using form's hidden element is only way u have like this-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<input type="hidden" name="country" value="Norway">
<input type="submit" value="Submit">
</form>
<p>Notice that the hidden field above is not shown to a user.</p>
The question is how u can use it with PHP, right?
The solution is here-
//In PHP
if( isset($_GET['fromPerson']) )
{
echo $fromPerson;
}
So combined HTML and PHP code will be like this (assuming a get element from prevous page is named fromPerson)-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<?php
if( isset($_GET['fromPerson']) )
{
echo '<input type="hidden" name="country" value=".$_POST['fromPerson'].">';
}
?>
<input type="submit" value="Submit">
</form>
Lets say you get a parameter p1 from a get request, it should look like this:
http://server.com/?p1=123
In your form, you can add hidden fields that would have the same effect when you submit, like this:
<form method="GET">
<input type="hidden" value="<?php echo $_GET["p1"]; ?>" name="p1">
</form>
That way you can resend the variables as many times as you need.
I'm not sure I understand your question... Can you post your code?
I assume you mean something like this?
in index.php
<input type="hidden" name="id" value="<?php echo $id; ?>" />
in return.php
Edit
I am a new to this type of coding so I was wondering if someone could help me.
Here's what I want to achieve, an input field where the user can enter text and have another text appended to this. So for example, when the user enters text e.g "My Name!!", upon posting there would be another hidden text appended to this, so the server would receive "Hidden Text!","My Name!!".
Here's an image explaining this in an easier way.
Here is my code so far..
<form method="post" action="jumpin.php">
<label>Desired Username:</label>
<div>
<label id='labletext'><?php echo $_SESSION['user_name_custom']; ?></label>
<input type="text" id="userid" name="userid" />
<input type="submit" value="Check" id="jumpin" />
</div>
<script>
$('#userid').keyup(function(){
$(this).css('color','#000');
});
$('#userid').blur(function(){
var value = $('#labletext').text()+$(this).val();
$(this).val(value);
});
</script>
</form>
This code doesn't seem to be working, all the server receives is the text the user submitted and not the "labletext".
You can use a hidden input field.
<input type="hidden" name="extra_label" value="<?php echo $_SESSION['user_name_custom']; ?>" />
Not visible to your users, but the data is passed to your server.
In your server-side code, you'll access the variable like $_REQUEST['extra_label'].
<input type="hidden" name="label" value="yourvalue" />
And in your submission you can do this
if(isset($_POST['submit']))
{
$text = $_POST['text'];
$label = $_POST['label'];
$string = $label.$text;
}
You have to put the concatenated value into the form field so it will be submitted to the server when you post the form.
$('#userid').blur(function(){
var value = $('#labletext').text().trim() + ' ' +$(this).val();
$(this).val(value);
});
Well, I have 2 php files. The first one user form.php has an html form to collect user inputs and send those data to other php file mysql_insert.php via POST method.
The problem is that along with 6 user inputs, I also have to transfer one more variable count(as discussed in code below) which is not input from user.
I tired following approach:
user form.php--->
<?php
$count=file_get_contents("cnt.txt","r");
$count=$count+1;
echo"Welcome!<br/>You are student number $count.<br/>";
?>
<html>
<p>Fill in the following information to save your marksheet to the database:</p><br/>
<form action="mysql_insert.php" method="POST">
Name:<input type="text" name="name" value=""/><br/><br/>
Marks(out of 100)<br/>
Subject 1:<input type="text" name="sub1" value=""/><br/>
Subject 2:<input type="text" name="sub2" value=""/><br/>
Subject 3:<input type="text" name="sub3" value=""/><br/>
Subject 4:<input type="text" name="sub4" value=""/><br/>
Subject 5:<input type="text" name="sub5" value=""/><br/><br/>
<p name="count" value="$count"></p>
<input type="submit" value="Submit"/>
</form>
</html>
I simply named an empty P element as count and set its value as $count to send it along with input variables(correct me if there is something wrong here, I am very novice to php).
And on the receiving end I used following code:
mysql_insert.php--->
<?php
require("connect.php");
$name=$_POST['name'];
$s1=$_POST['sub1'];
$s2=$_POST['sub2'];
$s3=$_POST['sub3'];
$s4=$_POST['sub4'];
$s5=$_POST['sub5'];
$count=$_POST['count'];
.
.
.
?>
Now on the line $count=$_POST['count']; the browser is throwing the error:
Notice: Undefined index: count in C:\xampp\htdocs\Vikas-117-PHP\level 3\mysql_insert.php on line 10
It seems the count is not being posted to this file.
Please guide me where I am wrong.
P.S.: I can of course use the file_get_contents() in the mysql_insert.php and get the count value directly in this file, but I am considering that way as my last option.
So please help if the non user-input variable can be posted via forms???
Thanks a million!!!
you have to put your $count variable in a hidden field in your form.. The data which are put in form inputs only get posted in form submit..
<input type="hidden" name="count" value="<?php echo $count ?>">
<p name="count" value="$count"></p>
you can not use directly any html tag for post data only use html form fields for posting data.
for use html tag values post you need to use js/ajax/php
<p name="count"><?php echo $count;?></p>
or better you use
<textarea name="count"><?php echo $count;?></textarea>
or for data not showing use hidden field
and get by name this p value or use class or id for get and post data using ajax
<p name="count" value="$count"></p>
This is not an input element so you cant post directly. You can however use jquery to get the value and POST using js.
You need to have the value inside input type text or hidden if you want to do the way you are doing now.
Change this line : <p name="count" value="$count"></p>
to the : <input type="hidden" name="count" value="<?=$count?>" />
You can use hidden elements like the following
use
<input type="hidden" value="<?php echo $count; ?>" name="count"/>
instead of
<p name="count" value="$count"></p>
Instead of
<p name="count" value="$count"></p>
Use
<input type="hidden" name="count" value="<?php echo $count;?>">
this line will not work : <p name="count" value="$count"></p>
you can use <input type="hidden" name="count" value="<?php echo $count; ?>" />
I have a text box which picks up a value from my database and is a read only text box. it contains some information which i want to send to my datbase but nothing is being recorded. Once the textbox isnt a read only, the data is successfully stored. Is there any way i can keep my textbox disabled and still send the data to my database?
<form action="refnoadded.php?public=<?php echo $id; ?>" method="post">
<span id="sprytextfield1">
<input type="text" name="ref" value="<?php echo $newpass ?>" disabled />
<input type="button" onClick="history.go(0)" value = "Generate Reference">
<br />
<span class="textfieldRequiredMsg">Reference Number Required</span>
</span>
<br />
<input type="submit" value="Add Reference" />
</form>
any ideas?
disabled doesn't send data to server
use
<input readonly value="my value" name="myname">
HTH!
have the data in a second hidden input:
<input type="hidden" name="ref_hidden" value="<?php echo $newpass ?>" />
the user won't see the difference and you will get your value send when submitting the form.
Disabled textfields don't submit their information to $_POST or $_GET. You can simply use the form element
<input type="hidden" name="rev_hidden" value="<?php print $password; ?>" />
This is the standard (correct) way to pass hidden information in the form.
Another use for this element is if you want to pass a "formsubmitted" variable.
However, if you want to create a value and have it uneditable by the user, do it on the server side. Create the value when you create the database, since users can relatively-simply send other data in the place of what you've generated.
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>