0

I'm an HTML newbie who is struggling to come up to speed fast. In the below code, I want the user to be able to enter a text string into the text field, then click "Submit". When the button is clicked, the user's string is "passed" to PHP code, which simply prints out the string for POC:

<html>
        <body>

        <form method="post">
                Input a String:  <input type="text" name="userInput"><br>
                <input type ="submit"/>
        </form>

        <?php
                if(array_key_exists('userInput', $_POST)) {
                        function1();
                }
                function function1() {
                        $userInput = $_POST['userInput'];
                        echo "User input was: \"$userInput\"\n";
                }
        ?>

        </body>
</html>

FULL DISCLOSURE :: This is code adapted from here. I'm pretty sure the PHP code is not being engaged, but I can't figure out why. Thank you.

EDIT :: As per @Barmar, I've added $userInput = $_POST['userInput']; to the PHP function. The code still doesn't work, alas.

Pete
  • 1,511
  • 2
  • 26
  • 49
  • 1
    It looks like it should work. Is it in a `.php` file? – Barmar Apr 27 '22 at 17:53
  • 1
    What do you see when you submit the form? – Barmar Apr 27 '22 at 17:54
  • 1
    If you input some text and submit it the text will be available as $_POST['userInput']. You can also upload the value using AJAX. – user3425506 Apr 27 '22 at 18:00
  • 1
    Would be an idea to 1. name the function with something meaningful, and 2. use arguments so you can use it from several contexts, instead of locking it to a variable outside its scope. ```function say_user_input($input) { echo "User input was: \"$input\"\n"; } if(!empty($_POST['userInput'])) { say_user_input($_POST['userInput']); }``` – Markus AO Apr 27 '22 at 18:10
  • @Barmar Thanks Barmar. Actually, you see the entire HTML file in this post. For once, I was able to crunch it down to SO postable code. Then this webpage loads, you can see the "Input a String: " text, the text box, and the SUBMIT button. But when you type in a string and click SUBMIT, nothing happens. – Pete Apr 27 '22 at 20:28
  • @MarkusAO Thanks Markus! I'm in meetings all day today, I'll try your solution first thing tomorrow, will let you know how it goes – Pete Apr 27 '22 at 20:29
  • 1
    You never set the variable `$userInput`. You need `$userInput = $_POST['userInput'];` in the function. – Barmar Apr 27 '22 at 20:35
  • @Barmar Thanks Barmar. See OP, now edited with your added line. I'm afraid that didn't do the trick, however... – Pete Apr 27 '22 at 20:40
  • 1
    I said "in the function". If you do it outside the function you need `global $userInput;`. You need to read a tutorial on PHP variables and scopes. – Barmar Apr 27 '22 at 20:42
  • 1
    And you shouldn't set the variable until after you check if the key exists with `array_key_exists`. – Barmar Apr 27 '22 at 20:42
  • @Barmar Ah, so you did say in the function. I've revised the edit and the code. It is still not working, I'm afraid, but thank you – Pete Apr 27 '22 at 20:45
  • 1
    Are you running this on an actual server that's running PHP, or just locally in your browser? Assuming it's on a web server, have you checked the logs? Can you run a simple PHP script of ``? – j08691 Apr 27 '22 at 20:48
  • @j08691 Thanks j08691. My server has PHP installed, version 8.1.2. I am testing by pointing a browser at the server and clicking a link which loads the above code. – Pete Apr 27 '22 at 20:51
  • Do you know what webserver (Apache, NGINX etc) you're using? Some of them have posted data disabled by default/ require configuring to make sure they actually pass the post body to php – Luke Briggs Apr 27 '22 at 21:13
  • The amended version works on my server. Is the file named with a PHP extension? Is your web server properly serving PHP pages? Copy the undernoted code into a file called test.php and run it, what do you get? – deep64blue Apr 27 '22 at 20:49

0 Answers0