Calling a particular PHP function on form submit

后端 未结 6 1245
星月不相逢
星月不相逢 2020-11-28 22:11

I was trying to call a particular php function in submit of a form both the form and php scripts are in same page. My code is below.(it is not working and so I need help)

相关标签:
6条回答
  • 2020-11-28 22:19

    Assuming that your script is named x.php, try this

    <?php 
       function display($s) {
          echo $s;
       }
    ?>
    <html>
        <body>
            <form method="post" action="x.php">
                <input type="text" name="studentname">
                <input type="submit" value="click">
            </form>
            <?php
               if($_SERVER['REQUEST_METHOD']=='POST')
               {
                   display();
               } 
            ?>
        </body>
    </html>
    
    0 讨论(0)
  • 2020-11-28 22:23

    In the following line

    <form method="post" action="display()">
    

    the action should be the name of your script and you should call the function, Something like this

    <form method="post" action="yourFileName.php">
        <input type="text" name="studentname">
        <input type="submit" value="click" name="submit"> <!-- assign a name for the button -->
    </form>
    
    <?php
    function display()
    {
        echo "hello ".$_POST["studentname"];
    }
    if(isset($_POST['submit']))
    {
       display();
    } 
    ?>
    
    0 讨论(0)
  • 2020-11-28 22:25

    Write this code

    <?php
        if(isset($_POST['submit'])){
            echo 'Hello World';
        } 
    ?>
    
    <html>
         <body>
             <form method="post">
                 <input type="text" name="studentname">
                 <input type="submit" name="submit" value="click">
             </form>
         </body>
    </html>
    
    0 讨论(0)
  • 2020-11-28 22:35

    PHP is run on a server, Your browser is a client. Once the server sends all the info to the client, nothing can be done on the server until another request is made.

    To make another request without refreshing the page you are going to have to look into ajax. Look into jQuery as it makes ajax requests easy

    0 讨论(0)
  • 2020-11-28 22:43

    If you want to call a function on clicking of submit button then you have
    to use ajax or jquery,if you want to call your php function after submission of form you can do that as :

    <html>
    <body>
    <form method="post" action="display()">
    <input type="text" name="studentname">
    <input type="submit" value="click">
    </form>
    <?php
    function display()
    {
    echo "hello".$_POST["studentname"];
    }
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
           display();
    } 
    ?>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-28 22:46

    you don't need this code

    <?php
    function display()
    {
    echo "hello".$_POST["studentname"];
    }
    ?>
    

    Instead, you can check whether the form is submitted by checking the post variables using isset.

    here goes the code

    if(isset($_POST)){
    echo "hello ".$_POST['studentname'];
    }
    

    click here for the php manual for isset

    0 讨论(0)
提交回复
热议问题