1

How can i call a PHP function from onchange on an HTML form textbox? I have tried

<input onchange="phpfunction()" ..... >

<?php
    function phpfunction() {
        my function
    }
?>

Any Help? I Can't seem to figure it out.

Thank You

xxx
  • 1,153
  • 1
  • 11
  • 23
varcor
  • 63
  • 1
  • 2
  • 10

4 Answers4

7

You have to do an ajax call and post that your PHP.

Example:

HTML:

<input name="blah" onchange="mainInfo(this.value);">

Javascript Function (AjaX)

function mainInfo(id) {
    $.ajax({
        type: "GET",
        url: "dummy.php",
        data: "mainid =" + id,
        success: function(result) {
            $("#somewhere").html(result);
        }
    });
};

PHP

<?php
    if(isset($_GET['mainid'])){
        mainInfo($_GET['mainid']);
    }
?>
jagmitg
  • 4,236
  • 7
  • 25
  • 59
4

You can't execute PHP in the browser.

Do an AJAX call or POST to a PHP function on the web server, or write a Javascript function that executes in the browser.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
0

you need to do it with JavaScript and send request to server. As HTML runs on client and php on sever.

Alishan Khan
  • 522
  • 1
  • 4
  • 13
0

You need to use jQuery for this, cant use PHP on client.

<input onchange="callphpfunction()" ..... >
<script>
callphpfunction()   {
    $.post("file.php",function(data) {
        alert(data);
    });
}
</script>

file.php

<?php
function phpfunction() {
    echo $data;
}
phpfunction();
Peppermintology
  • 9,343
  • 3
  • 27
  • 51
A.B
  • 20,110
  • 3
  • 37
  • 71