Page 1 of 1

Get sum with php

Posted: Mon Nov 18, 2013 11:33 pm
by tqmd1
Derar Sir,

I have follwoing codes

Code: Select all

<html>
  <head>
    <title>Javascript function </title>
	
    <style type="text/css">
	.box
	{
		width:400px;
		background-color:#F0F8FF;
	}
	
	h4
	{
		color:#09F
	}
		
	</style>
    
<script type="text/javascript">
function hello(){
var xx=eval(form1.text1.value);
var yy=eval(form1.text2.value);
form1.text3.value=xx+yy
}
</script>
	
  </head>
    <body onLoad="form1.text1.focus()">  
	<center>
    <div class="box">
	<h1 style="color:#2c80d3 ">Javascript Function</h1>
	<table border="0" cellspacing="1" cellpadding="1" width="25%"> 
    	<form name="form1" action="textboxes.php" method="Post">
    <tr><td> First</d><td width="20px"><input type="text" name="text1"  value=""></td></tr>
      <tr><td> Second</d><td><input type="text" name="text2"  value="" onBlur="hello()"></td></tr>
	 <tr><td> Result</d><td><input type="text" name="text3"  value="" disabled=""></td></tr>
    </form>
	</table>
	<h4>Enter any digit in text1 and text2 and see result in text3</h4>

    	<h4>Is it possible to do above with php without submitting FORM?</h4>

    </div>
	</center>
    </body>
</html>
No problem, It works fine.
I used java script to sum two numbers.
Is it possible to add two numbers with php without using any submit button?

If yes then please guide me.

Image

Re: Get sum with php

Posted: Tue Nov 19, 2013 1:10 am
by requinix
Yes, with AJAX: you send a HTTP request to your PHP script, make it do the calculations, have it return the result, and make your Javascript deal with the result however you want.

But needless to say, that's completely ridiculous for just simple arithmetic.

Re: Get sum with php

Posted: Tue Nov 19, 2013 1:44 am
by tqmd1
Thanks Requinix for helping, do you mean I should continue javascript for calculation on form then submit form to database?

Re: Get sum with php

Posted: Tue Nov 19, 2013 2:16 am
by requinix
...Kinda. Do the calculation in the form so that the user can see what's going on, but make your PHP script do the calculation itself. A second time.

Why? Because what happens in the browser is completely under the user's control. Like me, if I were using your site. I could make it do whatever calculations I wanted and then send that data to your script. That's why your script has to do the calculation too: to make sure it has the right figures and to make sure I can't cause something to happen that shouldn't.

Classic example is on a shopping cart and adjusting item totals with Javascript. If I want two items then go ahead and show me the original price *2, but don't actually use that particular number to decide the total price. Otherwise I could just make it "calculate" $0.00 and get your stuff for free.