开发者

Printing text box value on page in PHP

I am new to PHP (in-fact I am learning it). I am开发者_JAVA技巧 trying to get the value of textbox defined in HTML and print it on HTML page through PHP code. I am able to send the value of textbox to another page using form post and getting it using $_POST['name'] on other page.

I am not able to print the value of textbox on same page.

Here is my code:

<html>
    <head>
        <?php

            function myfunction()
            {
                printf($_POST['fname']);
            }

        ?>
    </head>

    <body>

        Name: <input type="text" name="fname" />

        <input type="button" onClick="myfunction()" value="Click" />

    </body>
</html>


First of all it's not required to write php function within <head> tags. Second, you can't POST data without <form> tags. try this

<?php
if(isset($_POST['submit']) && $_POST['submit']=='Submit'){
     $name=$_POST['fname'];
     echo $name;
     }
else {  
        ?>
<html>
    <head>

    </head>

    <body>
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">
        Name: <input type="text" name="fname" />

        <input type="submit" name="submit" value="Submit"/>
</form>
    </body>
</html>
<?php } ?>


you can not call PHP function by button's click event...

if you want to print on same page then you can do by below code..

<html>
    <?php
        if(count($_POST)>0){
            echo $_POST['fname'];
        }
    ?>
<body>
  <form method='post'>
    Name: <input type="text" name="fname" />

    <input type="submit" value="Click" />
  </form> 
</body>
</html>

hope you will get what you want by my answer..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜