-2

I am trying to execute a PHP function when a HTML button is clicked, to change the text of a HTML label, but my code is not working.

Below is my code:

Can someone please tell me what I am doing wrong? Thanks a lot for any help.

     <form>
        <input type="number" name="score"><br>
        <input type="submit" name="Submit" class="btn btn-primary" action="calcScore">
    </form>


    function calcScore()
{
   $playerThrow = $_GET[$score];
   return $playerThrow;
}


<label class="scorePlate"><?php echo calcScore(player1) ?></label>
user7554035
  • 389
  • 2
  • 5
  • 19
  • 1
    You need JavaScript for that. – Jay Blanchard Aug 22 '17 at 15:09
  • 1
    @JayBlanchard — No, they don't. – Quentin Aug 22 '17 at 15:10
  • what's player1 is a var? if it isn't and should be treated as a string - then it needs quotes – treyBake Aug 22 '17 at 15:10
  • 1
    They do if they don't want to reload the page @Quentin, which is what this smells like. – Jay Blanchard Aug 22 '17 at 15:10
  • Lot of question: a) From Where `select()` function coming? b) From Where `calcScore(player1)` coming? c) Where have you written any text change code? d) `$_GET[$score]` is this the way to retrieve? – Nana Partykar Aug 22 '17 at 15:11
  • 1
    @JayBlanchard — They haven't mentioned not reloading the page – Quentin Aug 22 '17 at 15:11
  • I want to be able to click the HTML button, to trigger text change of a label. What do you mean by reloading the page? – user7554035 Aug 22 '17 at 15:12
  • Unless you're using AJAX, your form will submit when you click the button and the page will reload. – j08691 Aug 22 '17 at 15:13
  • PHP is compiled and executed every page refresh. For you to change the HTML button/label/anything in php, you need to recall the page. Javascript/Jquery would be the ideal way to handle this. – IsThisJavascript Aug 22 '17 at 15:14
  • You should be answering the questions that @NanaPartykar posted as they are crucial to your logic – IsThisJavascript Aug 22 '17 at 15:15
  • @NanaPartykar I have changed the function name. To complete the functionality completely, I want to pass 2 values from Dropdowns to calculate the score – user7554035 Aug 22 '17 at 15:17
  • I know they haven't mentioned it @Quentin, but my spidey-sense tells me this is what they want. – Jay Blanchard Aug 22 '17 at 15:20
  • @user7554035 you're making several mistakes in your code and I recommend you spend some time reading about what a $_GET is and how a [function works](http://php.net/manual/en/functions.user-defined.php) For example, you're not wrapping your function in PHP tags, how on earth do you expect your websever to understand what part is PHP and the other is HTML? You're also passing a constant to a function that's not accepting one. – IsThisJavascript Aug 22 '17 at 15:21
  • Can I execute SQL statements in Javascript? When I get this working, I will need to be able to retrieve data from SQL table & display as a label – user7554035 Aug 22 '17 at 15:30
  • @user7554035 — If you want to talk to an SQL database server, then you can use JavaScript, but you'll need it to be server side JavaScript (e.g. via Node.js). Since you're already using PHP, there doesn't seem to be much reason to change. – Quentin Aug 22 '17 at 15:46
  • @Quentin So stick to using PHP to connect to the database? – user7554035 Aug 22 '17 at 15:52
  • use javascript. Its a better option than php in this case: `document.getElementById("class_name").innerHTML("whatever-you-want-here");` – Explosion Aug 22 '17 at 16:18

2 Answers2

3
  1. You have passed the player1 constant to the function, but you haven't declared it (except implicitly) nor have you used it. Remove it, it is pointless.
  2. The field name is score. It is not the value of the $score variable (which you also haven't defined). Use a string literal, not a variable name.
  3. You have to define your function inside a <?php ?> block
  4. You have to use the same name for the function when you create it as when you call it
  5. You need to test if the value exists (to avoid PHP throwing a warning) and provide appropriate behaviour if it does not
  6. You need to encode user input as HTML to avoid creating an XSS security hole
  7. Input elements have no action attribute. Remove it, it is pointless. (They do have a formaction attribute, which lets a submit attribute override the normal action of the form, but it doesn't make sense to do that here).

Such:

     <form>
        <input type="number" name="score"><br>
        <input type="submit" name="Submit" class="btn btn-primary">
    </form>

<?php
    function calcScore() {
        if (isset($_GET["score"])) {
            $playerThrow = $_GET["score"];
        } else {
            $playerThrow = "default";
        }
    return $playerThrow;
    }
?>

<label class="scorePlate"><?php echo htmlspecialchars(calcScore()) ?></label>

Aside from this: The point of a label is to describe the purpose of a form control (input, select, etc). It makes absolutely no sense to use it for this. You should pick a more appropriate element (and add a label element to describe your score field).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I just want to get the text displaying on a label first, i will then change the element type. I've copied your code in, but it is still not rendering for me. – user7554035 Aug 22 '17 at 15:24
  • 1
    @user7554035 — I created a working test case and copy/pasted the code from it into this answer. If it isn't working for you, that suggests the problem is that you haven't set up a suitable PHP environment. [Answers to this question describe how to troubleshoot that](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page). You should also create a test script consisting solely of `` to test it, so it isn't being muddled by code that depends on user input. – Quentin Aug 22 '17 at 15:25
-1

The correct method of doing this is -

<?php
$var = $_POST['score'];
 ?>
 <html>
 <form method="post">
    <input type="number" name="score" /><br>
    <input type="submit" name="Submit" class="btn btn-primary" />
    </form>
<label class="scorePlate"><?php echo $var ?></label>
</html>
  • This reads more like a game of spot the difference than an answer. What have you changed? Why should it solve the problem? – Quentin Aug 22 '17 at 15:19
  • **Danger**: This code is [vulnerable to XSS](https://www.owasp.org/index.php/XSS) User input needs escaping before being inserted into an HTML document!. – Quentin Aug 22 '17 at 15:19
  • @Quentin i was typing...and then after posting it,there was your answer... – Devanshu Goyal Aug 22 '17 at 15:21