2

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.

4
  • this is java script function "document.address.value"; you have to write it in script tag Commented May 20, 2015 at 5:14
  • Use html form... read about it Commented May 20, 2015 at 5:14
  • learn as to how HTML & Javascript are different , You need to use ajax to do the above thing Commented May 20, 2015 at 5:15
  • 1
    Use form post and get value of Address using $_POST['address']. As @saty said document.address.value is JS function. Commented May 20, 2015 at 5:17

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

@Loenix, yup. That was an alternative but removed that and edited.
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.