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.
Related
I have multiple inquiry forms all of which call the same file used for email forwarding, so it's titled emailForwarding.php. I apparently managed to separate the forms using jQuery on the front end, but the script in emailForwarding.php is processed the same number of times as the number of the inquiry forms. I just want the php script to work for the form I submit.
I tried isolating the effect of the script using .eq() and .index() and passing an argument named $arg to only trigger form submission event for the div.vendor-wrapper containing the selected form.
single.php:
echo
'<div class="vendor-wrapper"><form method="post" action="" name="form" class="commentForm">
<textarea name="comment" placeholder="Please enter your message in the space of 300 characters and hit the Confirm button." value="" class="message" maxlength="300"></textarea>
<input name="confirm" type="button" value="Confirm">
<input class="send" name="send'.$i++.'" type="submit" value="Send">
<input type="hidden" name="position" val="">
</form></div>;
<script>
$('.confirm').click(function(){
$('.vendor-wrapper').find('.position').val('');
var index = $(this).parents('.vendor-wrapper').index()-1;
if($('.vendor-wrapper').eq(index).find('.message').val()){
$('.vendor-wrapper').eq(index).find('.confScreen').show();
$('.vendor-wrapper').eq(index).find('.position').val(index);
}
});
</script>
emailForwarding.php:
if(isset($_POST['position'])):
$arg = 'send';
$arg .= $_POST['position'];
echo "<script>console.log('PHP: ".$arg."');</script>";
if(isset($_POST[$arg])):
if(isset($_POST['comment'])):
$EmailCustomer = $_POST['email'] = $current_user->user_email;
//The rest of the script for email processing omitted.
The form is submitted the same number of times as the number of the forms on the page.
Inserting include() before tag of single.php disabled duplicate submission.
Could you provide more code? Because I was trying to reproduce the problem but could not with the provided code. As, what $_POST['position'] stands for is not clear from code.
Is the echo statement user any loop. Can you try by giving a different name to FORM?
<form method="post" action="" name="form-$i" class="commentForm">
I want to have 2 input fields and a button. Once you press the button it adds the two numbers together and shows the result below. The input numbers have to be between 0 and 10. If one of the inputs is 0, the sum has to be 0 no matter what.
Here's what I have so far:
<form action="" method="post">
<label>Enter Num1:</label>
<input type="text" name="num1" /><br>
<label>Enter Num2:</label>
<input type="text" name="num2" /><br><br>
<input type="submit" name="btn_submit" value="Add">
</form>
<?php
if(isset($_POST['btn_submit']))
{
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$total = $num1+$num2;
echo "The total value is: ".$total;
}
?>
The first problem is that when I press the 'Add' button, my input fields are cleared and nothing happens. The second problem is that I have no clue on how to add the 2 mentioned conditions. Can anyone help me out?
The first problem is that when I press the 'Add' button, my input fields are cleared and nothing happens
This how it works. If you want your <input>s to be pre-filled with any values, you must do that yourself. There's value argument for any <input> you can use.
The second problem is that I have no clue on how to add the 2 mentioned conditions.
there's no additional conditions clear in your question. You just added two values in proper way, so it's unclear what you are having problem with here.
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!!
I am just trying to learn PHP and want to get the value of the textbox using $_post function, but its not working. I am using wamp 2.1 and the code is simple as below
<form method="POST" action="c:/wamp/www/test/try.php">
<input type="text" name="nco" size="1" maxlength="1" tabindex="1" value="2">
<input
tabindex="2" name="submitnoofcompanies" value="GO"
type="submit">
</form>
<?php
if (!isset($_POST['nco']))
{
$_POST['nco'] = "undefine";
}
$no=$_POST['nco'];
print($no);
However in no way I get the value of the textbox printed, it just prints undefined, please help me out.
You first assigned the word "undefine" to the variable $_POST['nco'].
You then assigned the value of the variable $_POST['nco'] (still "undefine" as you stored there) to the variable $no.
You then printed the value stored in the variable $no.
It should be clear that this will always print the word "undefine".
If you want to print the value of the textbox with the name nco, fill out the form with that textbox, and in the page that process the form,
echo $_POST['nco'];
...is all you do.
You need to setup a form or something similar in order to set the $_POST variables. See this short tutorial to see how it works. If you click the submit button, your $_POST variables will be set.
what for you are using this line $_POST['nco'] = "undefine"; } ..?
and please cross check whether you are using form method as post and make sure that your text name is nco ... or else use the below code it will work.
<?php
$no = $_POST['nco'];
echo $no;
?>
<form name='na' method='post' action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type='text' name='nco'>
</form>
thanks
Your action is wrong.
Change it to
action="try.php"
Web site: www.matka-opas.com/ahaa4.php
If i push SEARCH-1 and then SEARCH-2 it works ok,
but how it could do by one search button ?
<?php
$depairport = $_POST['depairport'];
$ddestination = $_POST['ddestination'];
$ddday = $_POST['ddday'];
$depmonth = $_POST['depmonth'];
$depyear = $_POST['depyear'];
$retday = $_POST['retday'];
$retmonth = $_POST['retmonth'];
$retyear = $_POST['retyear'];
$aikuisia = $_POST['aikuisia'];
$osoite = $depairport . $ddestination . $ddday . $depmonth . $retday . $retmonth;
?>
<form method="post">
<input type="submit" value="SEARCH 1">
thanks ! ;]
If i push SEARCH-1 and then SEARCH-2
it works ok, but how it could do by
one search button ?
Just specify one search button and it will submit the form it is under. And you shouldn't be specifying more than one submit button for a particular form.
Here is what you should have:
<form method="post" action="">
.............
.............
.............
<input type="submit" value="SEARCH">
</form>
if you want to submit your form to http://www.travelstart.fi/combo/ you should put the url in the action attribute of the form. Your form will be posted to that url when it is submitted with <input type="submit" value="SEARCH">
<form method="post" action="http://www.travelstart.fi/combo/">
You also might want to learn more about semantic web and the separation of content and markup. I can understand you want to use tables in this kind of layout but 3 <br> to create a top margin? I saw you included a stylesheet in your page so...
EDIT: Now that your question is updated I think I understand it better. If you don't need any server-side functionality to calculate the url it might be better to put this functionality in javascript. This means it will be executed inside the browser. This saves you a round-trip to the server and so makes your page navigate a lot faster. This is a quick setup to get you started:
<form id="myForm">
...
<input type="submit" />
</form>
<script type="text/javascript">
myForm.onsubmit = function() {
window.location = 'http://www.travelstart.fi/combo/' + myForm.depairport + myForm.ddestination + myForm.ddday + myForm.depmonth + myForm.depyear + myForm.retday + myForm.retmonth + myForm.retyear + myForm.aikuisia;
return false;
}
</script>
As a disclaimer: this is not the best way to program it, it is just a short example to guide you in the right direction. Getting the right javascript code for what you want to achieve is a different question on SO.
It will probably not work right away since I don't think that will result in a valid url but I hope you get the picture. To make it cross-browser compatible you might want to use a javascript library like JQuery.
if you are trying to submit data
<input type="submit" value="SEARCH 1">
and then go to different url
to combine this you can use php that would detect a submit and redirect user to a different page...
as example:
$depairport = $_POST['depairport'];
$ddestination = $_POST['ddestination'];
$ddday = $_POST['ddday'];
$depmonth = $_POST['depmonth'];
$depyear = $_POST['depyear'];
$retday = $_POST['retday'];
$retmonth = $_POST['retmonth'];
$retyear = $_POST['retyear'];
$aikuisia = $_POST['aikuisia'];
$osoite = $depairport . $ddestination . $ddday . $depmonth . $retday . $retmonth;
if (isset($_POST['Submit'])) {
header( 'http://www.travelstart.fi/combo/'.$osoite ) ; }
you might want to change html just to:
<input type="submit" name="Submit" value="SEARCH 1">
thats all - make sure You are using POST method submiting the data