Conversion to php

Dear Sir,

I have following code

[code]

Javascript function

Javascript Function

First
Second
Result

Enter any digit in text1 and text2 and see result in text3

[/code]

It works fine. I want to ask is there any method to convert above javascript function to php function.
I want to make a php function and then call that from textbox’s onblur event.

Please help

textboxes.php
[php]<?php

$text1 = isset($_POST[‘text1’]) ? (int) $_POST[‘text1’] : ‘’;
$text2 = isset($_POST[‘text2’]) ? (int) $_POST[‘text2’] : ‘’;
$result = ‘’;

if (!empty($_POST)) {
if ($text1 == $text2) {
$result = ‘The values are the same’;
} elseif ($text1 >= 0 && $text2 >= 0) {
$result = ‘Amount ’ . ($text1 > $text2 ? ‘1’ : ‘2’) . ’ is greater’;
} else {
$result = ‘Invalid data’;
}
}

?>

Javascript function

Javascript Function

First ">
Second " onblur="hello()">
Result " disabled="">

Enter any digit in text1 and text2 and see result in text3

[/php]

Thanks but sir on line 11 it says:

Object expected

What is this error message?

it’s a javascript error
[php]

Second <input type=“text” name=“text2” value="<?= htmlentities($text2, ENT_QUOTES, "UTF-8") ?>" onblur=“hello()”> [/php]

you don’t have the hello function anywhere, change to this

[php]

Second <input type=“text” name=“text2” value="<?= htmlentities($text2, ENT_QUOTES, "UTF-8") ?>" > [/php]

Thanks SIR for helping but your codes show result after pressing DISPLAY BUTTON.

Is not not possible to get result without pressing button like my javescript code in first post like this

<tr><td> Second</d><td><input type="text" name="text2"  value="" onblur="hello()"></td></tr>

Here when cursor loses focus from text2 then result is displayed immediately.

PHP runs server side in contrast to javascript that runs in the browser client side. So no, in regular circumstances you must refresh the page to run new PHP.

In such an easy example as this you’re better off just running it in javascript. If you do want to run it in PHP and have it “live” you should look into javascript ajax requests. With jquery this is very easy.

It works like this:
[ul][li]Javascript performs an ajax request to a PHP file[/li]
[li]PHP file returns data[/li]
[li]Javascript parse the returned data and insert it into the HTML-element where you want the data to show[/li][/ul]

So you should add the hello() function back to the HTML, then add a javascript function which does what I said above.

Thanks sir for helping, do you mean I should continue javascript for calculation on form then submit form to database?

Sponsor our Newsletter | Privacy Policy | Terms of Service