简体   繁体   中英

calling php function from submit button

I want to pass the value of input field address to php. So that when i click on submit button it will be store in php.

<input type="text" name="address">
<input type="submit" name="go" method="post">
<?php
    if($_POST){
    if(isset($_POST['go'])){
    call();
    }else{
    echo "Error";
    }
    }
    function call(){
    echo "hi";
    print(document.address.value);
    }
?>

However when i click on button it response nothing.

what you intend to do can be done as below method.

<form method="post" >

<input type="text" name="address">
<input type="submit" name="go">

</form>



<?php
    if($_POST){
    if(isset($_POST['go'])){
    call();
    }else{
    echo "Error";
    }
    }
    function call(){
    echo "hi";
    echo $_POST['address'];
    }
?>

You are mixing PHP and JavaScript. If you want a pure PHP solution you could do something like:

<?php
if(isset($_POST['go'])){
     $address = $_POST['address'];
     echo $address;
}
?>

<form method="POST">
  <input type="text" name="address">
  <input type="submit" name="go" method="post">
</form>

With PHP once the form is submitted you can access form values with $_POST so use $_POST['address'] instead of document.address.value

Try like this,this is working fine as per your requirement:

<html>
<body>
<form action="" method="post">
Address: <input type="text" name="name"><br>
<input type="submit" name="go" value="call" />
</form>

<?php
if (isset($_REQUEST['name'])) {
    call();
    }
?>
<?php
function call() {
 $address= $_POST['name'];
echo "Address:".$address;
exit;
}

Output:-

For output Click here

use this code

<input type="text" name="address">
<input type="submit" name="go" method="post">
<?php
    if($_POST){
    if(isset($_POST['go'])){
    call();
    }else{
    echo "Error";
    }

    function call(){
    echo "hi";
    echo $_POST['address'];
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM