2

So I am trying to make a select element update a MYSQL record every time the selection is changed. But the only way I was successfully able to trigger the PHP code is through an input button and not what is shown below. I need to be able to trigger the PHP code with the invisible input submit.

The whole point is to update PHP records through the select element and no button.

<form id="signin-form_id" class="panel" method="POST" style="margin-bottom: 0px;">
    <div class="input-group">
        <span class="input-group-addon">
            Coin
        </span>
        <select class="form-control" name="cointype" onchange="this.form.submit()">
            <option value="0" selected>Bitcoin</option>
            <option value="1">Litecoin</option>       
        </select>
    </div>
    <noscript><input type="submit" name="ReferralCoin"></noscript>
</form>

And the submit button is suppose to trigger a POST event in PHP seen here: Do not comment about MYSQL injection vulnerabilities.

<?php
if (isset($_POST['ReferralCoin']))
{
    $cointype = $_POST['cointype'];
    if($cointype == "0") 
    {
        $odb->exec("UPDATE users SET `coin` = '$cointype' WHERE `ID` = '$userid'");
    } 
    elseif($cointype == "1")
    {
        $odb->exec("UPDATE users SET `coin` = '$cointype' WHERE `ID` = '$userid'");
    }
}
?>

The problem is if I was to use an input button it works fine and triggers the php POST code, but when using the invisible input submit it does not trigger the PHP POST code. If anyone can help me find my mistake, much is appreciated!

joanolo
  • 6,028
  • 1
  • 29
  • 37
TymeBomb
  • 117
  • 2
  • 12
  • Show us your js code as well – Ikhlak S. Jul 26 '17 at 18:39
  • @user3284463 I do not have any JS code to go along with this, this is all that is to it. Am I missing something with Javascript? – TymeBomb Jul 26 '17 at 18:40
  • If you're not using a submit button to trigger the form post, then what are you using? Do you have JavaScript code which responds to the `select` element's change event to submit the form? Basically, if you want the form to be submitted, *something* has to submit it. – David Jul 26 '17 at 18:40
  • 3
    @David He's using ` onchange="this.form.submit()" ` – Mikey Jul 26 '17 at 18:41
  • Wait... where's the rest of your PHP code? [Turn on your PHP errors](https://stackoverflow.com/a/5438125/1022914) if you have not done so. – Mikey Jul 26 '17 at 18:44
  • @TymeBomb: Is the form not being submitted at all? Or is something else failing? The client-side code itself seems to submit the form just fine: https://jsfiddle.net/wo67Lnad/ – David Jul 26 '17 at 18:45
  • @Mikey No other PHP code is needed as the PHP part is fine, I have tested the submit form with a input button and it works, I am trying to submit the form on select change and have no button visible, if that makes sense? – TymeBomb Jul 26 '17 at 18:45
  • @David Yes the form still get's submitted, just seems as if it doesn't trigger the php code with it as it normally does, seeming the PHP code is fine. – TymeBomb Jul 26 '17 at 18:47
  • remove that line `if (isset($_POST['ReferralCoin']))` – Jeff Jul 26 '17 at 18:48
  • @Jeff Pretty sure without that it will run the code on page load and that is not what I want, it needs to update when selection is changed. – TymeBomb Jul 26 '17 at 18:49
  • 1
    then change it to `if (isset($_POST['cointype']))` – Jeff Jul 26 '17 at 18:50
  • Does the post request even happen? – Ikhlak S. Jul 26 '17 at 18:50
  • @Jeff Worked, thank mate! – TymeBomb Jul 26 '17 at 18:51

1 Answers1

3

You're checking for the submit button here:

if (isset($_POST['ReferralCoin']))

But if the submit button isn't being clicked (since you're submitting the form via JavaScript code instead) then it isn't included in the POST values. So $_POST['ReferralCoin'] isn't set.

You'd need to remove the check entirely, or check some other condition. (Such as whether $_POST['cointype'] is set.)

David
  • 208,112
  • 36
  • 198
  • 279