How to populate input fields with PHP - php

I'm trying to write a simple calculator html page where I ask for two numbers in separate text boxes, have the user click a button and then the result gets stored into a third text box. I'm trying to using PHP to resolve this but can't seem to display it the way I want.
the echo line works fine, but I don't want that;
From HTML
<form action="Add.php" method="get">
<input for='num1' type="text" name="num1" />
<input for='num2' type="text" name="num2" />
<input type="submit" value="Add Them" />
<input type="text" name="AnswerBx" id="SumTotalTxtBx"/>
</form>
Add.php
<?php
$num1 = $_GET["num1"];
$num2 = $_GET['num2'];
$sum = $num1+$num2;
//echo "$num1 + $num2 = $sum";
document.getElementById("SumTotalTxtBx").value = $sum;
?>

You can't mix PHP and JavaScript like that! One is run on the server the other on the client.
You have to echo the value into the value attribute of the text boxes like so
<input type="text" value="<?PHP echo $sum; ?>" />

You are mixing PHP and Javascript here, go back to the PHP manual at : http://php.net/manual and read out how php works and interacts with the user.
Javascript is basicaly run on the client side while PHP is run on the server. You REQUEST php something and it returns a new HTML page for you.
That being said, its a too broad topic to help you fix your error.
Good luck

Using jQuery you can solve it without doing post back's at Server like this
var num1 = parseInt($("input:text[name='num1']"));
var num2 = parseInt($("input:text[name='num2']"));
$('input:text#SumTotalTxtBx').val(num1+num2);

Okay, so PHP is awesomeness, but all of it's calculations are performed on the serverside, not the clientside. Using AJAX, you can execute some of the PHP code back against the server, but I think you may be more interested in javascript for your calculator.
<html>
<head>
<script>
function calculateSum()
{
num1 = parseInt(document.getElementById("num1").value);
num2 = parseInt(document.getElementById("num2").value);
sum = num1 + num2;
document.getElementById("sum").innerHTML = sum;
}
</script>
<body>
<input id="num1" type="text"/><br/>
<input id="num2" type="text"/><br/>
<button onclick="calculateSum()">Submit!</button>
<span id="sum">..the sum is..</div>
</body>
</html>
Javascript is really quite simple, and is absolutely wonderful when programming anything on the clientside. If you want to learn more, I'd suggest you read up on javascript over at w3schools! I hope that helps!!

Related

PHP/HTML - Setting a cap on a input using a variable in php?

I'm looking for a way to limit the amount of an input like this:
<?php
$Maximum = 10;
?>
<form action="" method="post">
Quantity: <input type="number" value="0" min="0" max=$Maximum step="0" data-number-to fixed="2" data-number-stepfactor="100" name="Quantity" />
<input type="submit">
</form>
But the Maximum would change depending on the database so it could be anywhere from 1 to 1000+ etc. Is there any possible way I could do this just limiting in the HTML or would I have to just use a PHP if statement after the number is submitted?
Assuming you are getting $maximum value from the DB. You can try to use maxlength attribute. No need to use JS. If you wanna be on the secure side that you gotta do another check on server side with the if statement as you mentioned above.
Try this:
<?php $Maximum = 10; ?>
<form action="" method="post">
Quantity: <input type="number" value="0" min="0" maxlength="<?php echo $Maximum; ?>" step="0" data-number-to fixed="2" data-number-stepfactor="100" name="Quantity" />
<input type="submit">
</form>
ref: http://www.w3schools.com/tags/att_input_maxlength.asp
I suggest jQuery Parsley plugin which have this feature which you need and a lot of other features which is useful for form validation.
http://parsleyjs.org/
here are example which you need http://parsleyjs.org/doc/examples/simple.html
Also you should have validation whith php, because this way is on client side and is not safe.
You can use JavaScript code to limit the value.
In your current code, there is no limit, the $Maximum variable is not used.
How are you going to receive the $Maximum from the database?
Depending on how these style classes are implemented, you can print PHP values inside html code like this:
<html> [...] the max value is <?php echo $Maximum; ?> from now on [...] <html>

Can we use onclientclick in PHP?

<script type="text/javascript">
function changeText2(){
var userInput = document.getElementById('userInput').value;
document.getElementById('boldStuff2').innerHTML = userInput;
return confirm('Hello!');
//return true;
}
</script>
<form method="get">
<p>Welcome to the site <b id='boldStuff2'>dude</b> </p>
<input type='text' id='userInput' value='Enter Text Here' />
<input type='button' OnClientClick="return changeText2()" value='Change Text'/>
</form>
Is this possible in PHP? I tried it but I get an error. Please suggest a solution.
Not exactly.
PHP doesn't have as complicated a template engine as ASP.NET.
You can simply use the HTML onclick attribute directly. There is no need to use a differently named one on the client because there isn't a server side attribute with the same name getting in the way.
Best practise, however, is to write unobtrusive JS and bind your event handlers programatically.

Calculating values with formulae from user input in PHP

I want to do an equation using the values I get from the input boxes called ampMin, voltMin and hastMin...
I'm not sure if it's a syntax problem or my method of approach is plain wrong.. Here's is an example of how the equation should look and work like with Excel.
What am I doing wrong? Thank you for your time!
EDIT: worth mentioning is that the whole block of code is within "strckenergi.php".
<html>
<title>Sträckenergi</title>
<body>
<h3>Svetsmetod: 111</h3>
<h4><i>Med K=0.8</i></h4>
<pre>
<form method="post" action="strckenergi.php">
Amp. Min <input type="text" name="ampMin"> Volt. Min <input type ="text" name="voltMin"> Hast. Min <input type="text" name="hastMin"> </pre>
<?php
echo "kJ/mm (minimum) = " . $qMin
$qMin = ( $ampMin * $voltMin ) / (( $hastMin * 1000 ) / $hastMin * 0.8));
?>
</body>
</html>
This works.
<html>
<title>Sträckenergi</title>
<body>
<h3>Svetsmetod: 111</h3>
<h4><i>Med K=0.8</i></h4>
<pre>
<form method="post" action="form.php">
Amp. Min <input type="text" name="ampMin"> Volt. Min <input type ="text" name="voltMin"> Hast. Min <input type="text" name="hastMin">
<input type="submit" value="submit"> </pre>
</form>
<?php
if($_POST){
$voltMin = $_POST['voltMin'];
$ampMin = $_POST['ampMin'];
$hastMin = $_POST['hastMin'];
$qMin = ( $ampMin * $voltMin ) / ( $hastMin * 1000 ) / $hastMin * 0.8;
echo "kJ/mm (minimum) = " . $qMin;
}
?>
You don't appear to be using forms correctly. If you want the browser do be able to display it right away, you should use JavaScript instead. PHP will require an additional pageload, or an AJAX request.
And in order to post the form, you need a submit button. Otherwise, the browser won't know what to do with it.
Further, your first PHP line needs a semi-colon, and your second one needs to be above the first - otherwise, the interpreter won't know what your value is when printing it, because it hasn't been calculated yet.
To be honest, I think you need to start by googling for how to construct an HTML form, then you can look up simple JavaScripts. Lycka till!
First off, as Joel Hinz says above, you need a submit button so the page knows when the user is done inputting and wants to send the form to the server for processing.
Second, you need to close the form with a tag.
Third, you're probably better off sticking with php at this stage; JavaScript can be a bit tricky and mighty frustrating for beginners.
This is a rough approximation of how your form should look.
<form method="post" action="strckenergi.php">
Amp. Min <input type="text" name="ampMin">
Volt. Min <input type ="text" name="voltMin">
Hast. Min <input type="text" name="hastMin">
<input type="submit" value="submit">
</form>
See http://www.tizag.com/phpT/forms.php for a clear explanation of forms.
OK first thing first. When the form is submitted, the values contained in the $_POST array aren't immediately available to the script on the server which processes the input.
For that you will need something like the following:
<?php
if($_POST){// this checks for the existence of the $_POST array, i.e. was something submitted
//now we're assuming a form was submitted
$voltMin = $_POST['voltMin'];
$ampMin = $_POST['ampMin'];
$hastMin = $_POST['hastMin'];
$qMin = ( ($ampMin*$voltMin)/($hastMin*1000)/($hastMin*0.8) );
echo "kJ/mm (minimum) = " . $qMin;
}// if($_POST)...
?>
Then you can process the elements, and print out the results of your calculations.
Oh, and drop the pre tags unless you really need them.

How to Auto Submit you Html form using PHP

Hi guys I have html form which is set to action = something.php. now i want to autosubmit this form 9 times when submit button on the form is clicked. can i do this using PHP. Thanks in advance.
Since PHP is a server side script, you cannot actually submit the form 9 times. You can, however, do whatever you're doing with the data on the server side 9 times in a for loop.
for($i = 0; $i < 9; $i++) {
// use $_POST here and do your stuff
}
do you want to submit the form 9 times to the same PHP script, or do you just want to handle the submission 9 times? Don't think you can do the former from PHP, but the later would be easily handled via a loop:
for ( $counter = 0; $counter < 9; $counter += 1) {
\\ do your stuff
}
Do you have a particular reason why you want to submit the form vs. just running through the logic multiple times?
No, you can't do this in PHP. At least not the way you are thinking of doing it (actual multiple POST actions from your form page). The action of posting will automatically take the user to the page you are posting to, not giving you an opportunity to POST again. You could of course POST to another script form the script you POSTed to, but I don;t think this is what you are asking.
To make actual repeated physical POSTs you would likely need to use AJAX methods.
HTML Page
<form id="form1" name="form1" method="post" action="form.php">
<input type="text" name="name" id="name" />
<input type="text" name="email" id="email" />
<input type="submit" name="button" id="button" value="Submit" />
</form>
PHP Page
<?php
for($i=1; $i<=9; $i++) {
// Your posting stuff
echo $_POST['name'];
echo '<br>';
echo $_POST['email'];
echo '<br>';
}
?>
This is the way you can use.

Using a JS variable inside php code?

I'm attempting to make it so that when you click a button, it'll add new fields to the page. I know that onclick can only take JS functions so I decided to try my luck making a JS script. At first I had tried to do
<html>
<head><title>multiple line test</title><head>
<body>
<script type="text/javascript">
var texters='';
var num=0;
function newInput(nomnom)
{
texters='';
num=nomnom+1;
for (var i=0; i<nomnom; i++)
{
texters+='<p>\
Please specify a file, or a set of files:<br>\
<input type="file" size="40">\
</p>\
<p>\
Caption : <br> \
<input type="text" size="30">\
</p>';\
}
document.write(texters+'<div>\
<input type="submit" value="Send">\
</div>\
</form>\
<br>' + '<input type="button" value="new entry" onclick="newInput(num)">' . '</body></html>');
}
newInput(num);
</script>
</body>
</html>
but that didn't work. So I tried instead to add a little bit of php being that I know it better. I tried this :
<html>
<head><title>multiple line test</title><head>
<body>
<script type="text/javascript">
var texters='';
var num=0;
function newInput(nomnom)
{
document.write(<?php
tex='';
for (var i=0; i<=(nomnom); i++)
{
tex.='<p>
Please specify a file, or a set of files:<br>
<input type="file" size="40">
</p>
<p>
Caption : <br>
<input type="text" size="30">
</p>';
}
echo tex . '<div>
<input type="submit" value="Send">
</div>
</form>
<br>' . '<input type="button" value="new entry" onclick="newInput(num)">' . '</body></html>';
?>);
}
newInput(num);
</script>
</body>
</html>
But I know that won't work because I can't get the variable I'm using for the number of fields to use out of the JS and into the PHP. Is there any way I could force JS to put that number in $_POST so I can retrieve it without having to make another form? Or is there a better way to do what I'm trying to do?
You can set a hidden input field on the form, and have the Javascript populate that.
You should try using jquery http://jquery.com/ or a simialr scripting library as it will make manipulating the DOM much easier. Using jquery you can create an onClick function that will do what you want in a few lines of code:
var onClickAddInput = function()
{
$('div#your_div_id').append('<input type="text" size="30" />');
}
If you need to add more input boxes you could loop the append statement and give the individual input boxes ids that equal the loop number.
As commented PHP is for serverside, javascript for clientside. html is the ui elements which either can create. If you need something done on the server, do it in PHP, if it needs to be done on the client, do it in javascript. Here is a sample of javascript for you.
function newInput(nomnom)
{
var tex='';
for (var i=0; i<=(nomnom); i++)
{
tex+='<p>Please specify a file, or a set of files:';
tex+='<br/><input type="file" size="40"></p>';
tex+='<p>Caption :';
tex+='<br/><input type="text" size="30"></p>';
}
document.getElementById('form_id').innerHTML += tex;
}
when this function is called, it will create a number of new inputs and add them to the form with the id "form_id".

Categories