Get sum with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tqmd1
Forum Newbie
Posts: 7
Joined: Sat Nov 09, 2013 1:07 am

Get sum with php

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Get sum with php

Post 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.
tqmd1
Forum Newbie
Posts: 7
Joined: Sat Nov 09, 2013 1:07 am

Re: Get sum with php

Post by tqmd1 »

Thanks Requinix for helping, do you mean I should continue javascript for calculation on form then submit form to database?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Get sum with php

Post 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.
Post Reply