Dimple
Dimple

Reputation: 453

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.

Upvotes: 2

Views: 7769

Answers (4)

RaMeSh
RaMeSh

Reputation: 3424

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

Upvotes: 1

Sourabh Kumar Sharma
Sourabh Kumar Sharma

Reputation: 2827

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'];
    }
?>

Upvotes: 3

Karthick Kumar
Karthick Kumar

Reputation: 2361

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'];
    }
?>

Upvotes: 0

aharen
aharen

Reputation: 627

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

Upvotes: 4

Related Questions