Php Number

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
php75
Forum Newbie
Posts: 8
Joined: Wed Nov 30, 2016 3:04 am

Php Number

Post by php75 »

Hi,
By using PHP i want to display numbers automatically once i enter the numbers in textbox/number/time without click submit button.How can i do it? :?:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Php Number

Post by requinix »

Be more specific. What numbers? What textbox? What button? Why do you need PHP for this?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Php Number

Post by Christopher »

Yes, we need more information. In general with web apps, if something happens automatically based on another event then Javascript is involved.
(#10850)
php75
Forum Newbie
Posts: 8
Joined: Wed Nov 30, 2016 3:04 am

Re: Php Number

Post by php75 »

thank you for your reply..Just i want to learn about it...For example By using Arithmetic operators If i type some numbers with or without decimal values in html text box then without clicking automatically displays some values in table content.

This is my doubt...If possible give me a source code with explanation....
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Php Number

Post by requinix »

The principle of "don't reload the page but have PHP do something" is called AJAX. Javascript makes a request to your server just like any regular URL, except it happens behind the scenes without the browser needing to change the current page.

However you might not need it at all. Like if you want to do addition, you can do that entirely in Javascript. No PHP needed at all.

Code: Select all

<div class="calculator">
	<input type="number" name="a">
	<select name="op">
		<option>+</option>
		<option>-</option>
		<option>*</option>
		<option>/</option>
	</select>
	<input type="number" name="b">
	=
	<input type="number" name="c" readonly>
</div>

<script type="text/javascript">
// using jquery
$(function() {
	$(".calculator").each(function() {
		var $a = $("[name='a']", this);
		var $op = $("[name='op']", this);
		var $b = $("[name='b']", this);
		var $c = $("[name='c']", this);

		var recalculate = function() {
			// eval is evil... but easy. and this is only a demonstration anyways
			$c.val(eval(parseInt($a.val(), 10) + $op.val() + parseInt($b.val(), 10)));
		};

		$a.keydown(recalculate);
		$op.change(recalculate);
		$b.keydown(recalculate);
	});
});
</script>
Untested but should be close.
Post Reply