0v3k Shi3ld3r 0v3k Shi3ld3r - 1 year ago 69
PHP Question

Button Form POST Not Working

I have a simple button that hides once the event is triggered

//Define attributes
echo'<input type="submit" id="toggler" name="add_friend"class=button
value="Add '. $output1['username'].' As A Friend ?" onClick="action();"/>
//Hide the button
var hidden = false;
function action() {

if(!hidden) {
document.getElementById('toggler').style.visibility = 'hidden';

The above works as it should no problems , However when I add form to get
for the button does not hide nor does my POST make it to

echo ' <form method="post" >

<input type="submit" id=toggler name="add_friend" class="button"

value="Add '. $output1['username'].' As A Friend ?" onClick="action();"/>

How can I make correct this so that the button hides and my POST is passed on to my isset code please .

if (isset ($_POST['add_friend'])){
//rest of my code once the button is clicked and hidden

Thanks in advance .

Answer Source

A couple things wrong with this. You have an extra double quote before 'method' in your form, and should also add action="#" to the <form> tag. This tells the browser to send the result of the form to the current page. It's also good practise to add a hidden field to send your data, rather than adding it to the submit button. Try this and see if it works.

if (isset($_POST['add_friend'])) {
echo '
<form method="post" action="#">
    <input type="hidden" name="add_friend" value="'.$output1['username'].'">
    <input type="submit" id="toggler" class="button" value="Add '. $output1['username'].' As A Friend ?" onClick="action();"/>       

Bear in mind this will essentially reload the page, so if you want to make an asynchronous request (EG, send some request without loading the page again) you will need to look into a solution with AJAX.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download