Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$_SERVER['REQUEST_METHOD'] == 'POST' is set even no submit

i have a page with 2 forms. This is for insert into db using PHP/PDO. The problem I'm facing is that the IF statement on the insert page is producing an error, even though I have not pressed the submit button. Have tried several ways with $_POST isset/ !==0 and so on. No luck so far.

Can there be a problem with the jQuery/FORM-combo? Maybe that's triggering the $_POST?

jQuery for choosing forms:

<script type="text/javascript">
$(document).ready( function() { 
$('#formSel').change( function() {
var id = $(this).val();
if( id != '-' )
{
    $('form').hide();
    $('#form'+id).show();
    //$('#myModal').modal('show');
}
});
});
</script>

The forms are set up like this:

<div>
 <select class="form-control input-small" id="formSel">
 <option value="-">Choose category</option>
 <option value="One">One</option>
 <option value="Two">Two</option>
 </select>
</div>

<!-- Form One -->
<form id="One" style="display:none" method="POST" action="insert.php">
<input type="text" id="aaa" placeholder="aaa">
<input type="text" id="bbb" placeholder="bbb">
<button type="submit" value="btn-One">Submit</button>
</div>

<!-- Form Two -->
<form id="One" style="display:none" method="POST" action="insert.php">
<input type="text" id="ccc" placeholder="ccc">
<input type="text" id="ddd" placeholder="ddd">
<button type="submit" value="btn-Two">Submit</button>
</div>

To insert the data I have a second page (insert.php) containing the PHP/PDO. This is set up to execute when submit button is pressed:

if ($_SERVER['REQUEST_METHOD'] == 'POST' && (!empty($_POST['btn-One']))) {
    //execute code
}
else {
    echo 'Error submit One';
}

if ($_SERVER['REQUEST_METHOD'] == 'POST' && (!empty($_POST['btn-Two']))) {
    //execute code
}
else {
    echo 'Error submit Two';
}

I'm all out of ideas and skills to see the solution. Hopefully someone can spot what is triggering the forms.

like image 617
ChrVik Avatar asked Jun 07 '26 10:06

ChrVik


1 Answers

You are probably missing name in your forms and replace </div> with </form>. And form ID too!

<div>
 <select class="form-control input-small" id="formSel" name="formSel">
 <option value="-">Choose category</option>
 <option value="One">One</option>
 <option value="Two">Two</option>
 </select>
</div>

<!-- Form One -->
<form id="formOne" style="display:none" method="POST" action="insert.php">
          ^
<input type="text" id="aaa" name="aaa" placeholder="aaa">
                            ^
<input type="text" id="bbb" name="bbb" placeholder="bbb">
                            ^
<button type="submit" value="btn-One" name="btn-One">Submit</button>
                                      ^
</form>
^^^^^^^

<!-- Form Two -->
<form id="formTwo" style="display:none" method="POST" action="insert.php">
          ^
<input type="text" id="ccc" name="ccc" placeholder="ccc">
                            ^
<input type="text" id="ddd" name="ddd" placeholder="ddd">
                            ^
<button type="submit" value="btn-Two" name="btn-Two">Submit</button>
                                      ^
</form>
^^^^^^^
like image 140
Thamilhan Avatar answered Jun 10 '26 04:06

Thamilhan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!