0

I've been searching around for an answer to my question. Many other people have asked the same question but did not get an answer to their question. The question is this.

I have the following code.

function follow_button(){
global $wpdb, $bp;
$loguser = $bp->loggedin_user->id;
$disuser = $bp->displayed_user->id;
if($loguser == $disuser){
} else {
$results = $wpdb->get_results("SELECT * FROM followers WHERE user = $loguser AND profile = $disuser");
if(empty($results)){
    echo '<form id="followsubmit" action="" method="post">
    <input type="hidden" name="follow_button" value="follow">
    <input type="submit" name="submit" value="follow"></form>';
} else {
   echo '<form id="followsubmit" action="" method="post">
    <input type="hidden" name="unfollow_button" value="unfollow">
    <input type="submit" name="submit" value="unfollow"></form>';

}
}
if( isset( $_POST['follow'] ) && strlen( $_POST['follow'] )) {follow_function();}
if( isset( $_POST['unfollow'] ) && strlen( $_POST['unfollow'} )) {unfollow_function();}
}
}

The above code works fine. But I ran into some problems when I started adding more forms to the same page such as this one listed above. My problem that im having is, if I were to click the Follow (or unfollow) button, all the other forms will also submit.

I realize where my problem was.

    if( isset( $_POST['justsomeform'] ) && strlen( $_POST['justsomeform'} )) {justsomeotherfunction();}

note: all my other forms have set value such as the one listed above. Because they are all set, and they all have a value, they would call the php function even though I did not click on them.

After researching, onclick I tried to put the function inside the form onclick action.. eg..

<form action="" method="post">
<input type="hidden" name="cheese" value="yellocheese">
<input type="submit" name="submit" onclick="' . doafunction() . '" value="I like Cheese"></form>

But that didn't work as planned. It would execute the function when the page loads. So I started the quest to find an answer to my problem and came across an example such as..

$( "#followsubmit" ).submit(function( event ) {
<?php follow_function(); ?>
event.preventDefault();
});

But that didn't work.. So, here I am asking for help.

In-case the question wasn't clear enough or I got a little sidetracked. Here it is again.

I have a html form. When I click "submit" I need a execute a php file.

note: "if(isset..." will not do.

Thank you in advance!

EDITED:

The following is how all the forms are formatted.

function post_options_content(){
if( isset( $_POST['profile_comment_post_button'] ) &&  strlen( $_POST['profile_comment_post_button'] )) {my_comment_content();}
if( isset( $_POST['profile_comment_post_button'] ) && strlen( $_POST['profile_comment_post_button'] )){my_wall_nav_adder();}
if( isset( $_POST['public_comment_post_button'] ) &&  strlen( $_POST['publiccomment_post_button'] )) {my_comment_content();}
if( isset( $_POST['public_comment_post_button'] ) && strlen( $_POST['public_comment_post_button'] )){my_wall_nav_adder();}
if( isset( $_POST['userspost'] ) && strlen( $_POST['userspost'] )) {postdatatodatabase();}
if( isset( $_POST['main_userspost'] ) &&  strlen( $_POST['main_userspost'] )) {main_activity_post_to_database();}
if( isset( $_POST['like'] ) && strlen( $_POST['like'] )) {like_wall_post();}
if( isset( $_POST['removemypost'] ) && strlen( $_POST['removemypost'] )) {removemypost();}
if( isset( $_POST['report_this_post'] ) && strlen( $_POST['report_this_post'] )) {report_this_post();}
if( isset( $_POST['nudge'] ) && strlen( $_POST['nudge'] )) {send_nudge_to_database();}
if( isset( $_POST['unfollow'] ) && strlen( $_POST['unfollow'] )) {unfollow_button_action();}
if( isset( $_POST['follow'] ) && strlen( $_POST['follow'] )) {add_follow_to_database();}
}add_action('post_options', 'post_options_content');

I add

do_action('post_options');

to the bottom of the page or function that is needed.. Here is an example.

function main_activity_wall_feed() {
global $wpdb, $bp;
$disuser = $bp->displayed_user->id;
$loguser = $bp->loggedin_user->id;
$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;
$limit = 15;
$offset = ( $pagenum - 1 ) * $limit;
$entries = $wpdb->get_results( "SELECT * FROM wallpost ORDER BY id desc LIMIT $offset, $limit" );
if( $entries ) {
$count = 1;
$class = '';
    foreach( $entries as $entry ) {
    $class = ( $count % 2 == 0 );
    // vars
    $bpurl = $entry->sendersurl;
    $sendersname = $entry->sendersname;
    $memberavatar = $entry->sendersid;
    $ownersid = $entry->sendersid;
    $hide_me = $entry->anon;
    $messageid = $entry->id;
    $contentmessage = $entry->message;
    $profileid = $entry->profileid;

        if ($hide_me == '1'){
            echo '<div style="float:left;"><img src="/wordpress/wp-content/plugins/truth-wall/includes/images/mystery-man-50.jpg"></div>';
            echo '<div id="loopmessage">';
            publicreportanddeletebutton($messageid, $loguser, $ownersid); <-form
            anonwhowrotetowho($ownersid, $profileid, $bpurl, $sendersname, $contentmessage);
            echo '</div>';
            echo '<div id="loopnavbar">';
            publiclikebutton($messageid, $loguser); <- Form
            public_comment_post_button($messageid, $memberavatar); <-Form
            echo '</div>';
            get_wall_comments($messageid);
            post_comment_form($messageid); <-Form
            echo '<hr />';
        }
        if ($hide_me == '0'){
            echo '<div style="float:left;">';
                echo '<a href="' . bp_core_get_user_domain( $memberavatar ) . '">' . bp_core_fetch_avatar ( array( 'item_id' => $memberavatar, 'type' => 'full' ) ) . '</a>';
            echo '</div>';
            echo '<div id="loopmessage">';
                publicreportanddeletebutton($messageid, $loguser, $ownersid); <- Form
                whowrotetowho($ownersid, $profileid, $bpurl, $sendersname, $contentmessage);
            echo '</div>';
            echo '<div id="loopnavbar">';
                publiclikebutton($messageid, $loguser); <-Form
                public_comment_post_button($messageid, $memberavatar);
            echo '</div>'; <- Form
                get_wall_comments($messageid);
                post_comment_form($messageid); <- Form
            echo '<hr />';
        }
$count++;
}
} else {
echo 'No posts yet';
}
$total = $wpdb->get_var( "SELECT COUNT(*) FROM wallpost" );
$num_of_pages = ceil( $total / $limit );
$page_links = paginate_links( array(
'base' => add_query_arg( 'pagenum', '%#%' ),
'format' => '',
'prev_text' => __( '&laquo; Prev', 'aag' ),
'next_text' => __( 'Next &raquo;', 'aag' ),
'total' => $num_of_pages,
'current' => $pagenum
) );

if ( $page_links ) {
echo $page_links;
}
do_action('post_options');
do_action('post_comment_options');
}
?>

I would like to use ajax.. But no luck. I am asking for a simple ajax example showing a form with 2 hidden values, when clicked, call a php function.

function truth_form() {
if ( is_user_logged_in() ) {
echo '<form action="" method="post">
<center><textarea rows="3" cols="65" maxlength="180" name="userspost"></textarea>
<div style="width:250px;padding-top:5px;padding-bottom:25px;">
<div style="float:left;"><input type="checkbox" name="hideme" value="1" /> Anonymous!</div>
<div style="float:right;"><input type="submit" name="submit" value="Post" /> </div></div></form></center>
<br />';
}
}

function main_truth_form() {
if ( is_user_logged_in() ) {
global $bp;
echo '<form action="" method="post">
<center><textarea placeholder="Remember, be nice!" rows="3" cols="65" maxlength="180" name="main_userspost"></textarea>
<div style="width:250px;padding-top:5px;padding-bottom:25px;">
<div style="float:left;font-size:15px;"><input type="checkbox" name="hideme" value="1" /> Anonymous!</div>
<div style="float:right;"><input type="submit" name="submit" value="Post" /></div></div></form></center>
<br />';
} else {
echo 'Please Log In!';
}
}

function follow_button(){
global $wpdb, $bp;
$loguser = $bp->loggedin_user->id;
$disuser = $bp->displayed_user->id;
if($loguser == $disuser){
} else {
$results = $wpdb->get_results("SELECT * FROM followers WHERE user = $loguser AND profile = $disuser");
if(empty($results)){
    echo '<form id="followsubmit" action="" method="post">
    <input type="hidden" name="follow_button" value="follow">
    <input type="submit" name="submit" value="follow"></form>';
    if( isset( $_POST['follow'] ) && strlen( $_POST['follow'] )) {add_follow_to_database();}
} else {
   echo '<form id="followsubmit" action="" method="post">
    <input type="hidden" name="unfollow_button" value="unfollow">
    <input type="submit" name="submit" value="unfollow"></form>';
    if( isset( $_POST["unfollow"] ) && strlen( $_POST["unfollow"] )) {unfollow_button_action();}
}
}
}

function profilereportanddeletebutton($messageid, $loguser, $ownersid){
global $wpdb, $bp;
if ( is_user_logged_in() ) {
if ($loguser == $ownersid) {
echo '<div style="float:right;"><form action="" method="post" ><input type="hidden" name="removemypost" value="' . $messageid . '"><input type="image" src="/wordpress/wp-content/plugins/truth-wall/includes/images/trash.PNG" title="Delete" name="submit"></form></div>';
} else {
$check_if_already_reported = $wpdb->get_results( "SELECT * FROM reported_posts WHERE postid = $messageid AND whoreported = $loguser" );
    if (empty($check_if_already_reported)) {
        if ( is_user_logged_in() ) {
        echo '<div id="deletebutton"><form action="" method="post" ><font color="red"><input type="hidden" name="report_this_post" value ="' . $messageid . '"><input type="image" src="/wordpress/wp-content/plugins/truth-wall/includes/images/red-flag.PNG" title="Report" name="submit"></font></form> </div>';
        }
    } else {
    echo '<div id="reportedbutton">Flagged</div>';
    }
}
}
}
function publicreportanddeletebutton($messageid, $loguser, $ownersid){
global $wpdb, $bp;
if ($loguser == $ownersid) {
echo '<div style="float:right;"><form action="" method="post" ><input type="hidden" name="removemypost" value="' . $messageid . '"><input type="submit" name="remove_wall_post_submit" value="Remove"></form></div>';
} else {
$check_if_already_reported = $wpdb->get_results( "SELECT * FROM reported_posts WHERE postid = $messageid AND whoreported = $loguser" );
    if (empty($check_if_already_reported)) {
        echo '<div style="float:right;"><form action="" method="post" ><font color="red"><input type="hidden" name="report_this_post" value ="' . $messageid . '"><input type="image" src="/wordpress/wp-content/plugins/truth-wall/includes/images/red-flag.PNG" title="Report" name="submit"></font></form></div>';
    } else {
    echo '<div style="float:right;"><font color="red" size="1">Reported</font></div>';
    }
}
}

That's only a few of them.

Yadigit
  • 41
  • 2
  • 7
  • Can you provide an example of how you had your multiple forms structured that were all submitting when you clicked one submit? – Taplar Apr 27 '15 at 23:27
  • I edited the question to give you an example of how the data is passed around. I hope that is enough information. – Yadigit Apr 27 '15 at 23:45
  • you could use ajax to execute php functions. is there any reason you are not using ajax – akr Apr 27 '15 at 23:55
  • I know I can use ajax to execute the php function. I just don't know how. I'll keep searching around for an answer. – Yadigit Apr 28 '15 at 00:09
  • Is there any specific reason as to why are you planning on using php script inside jquery? – shadab Apr 28 '15 at 00:23
  • I have a follow button.. When it's clicked it's going to call the add_follow_to_database function.. the code inside of add_follow_to_database is `$wpdb->insert('table', array('who' => $person1, 'from' => $person2 ) array( '%d', '%d',));` – Yadigit Apr 28 '15 at 00:26
  • I think you need to fix your processing logic along with your forms. There is no need to do ajax from what you are describing. – Rasclatt Apr 28 '15 at 01:27
  • I'll mess around with how everything is processed. Any suggestions? – Yadigit Apr 28 '15 at 02:01
  • What do all your forms look like? Can you add those as an edit? – Rasclatt Apr 28 '15 at 02:16
  • I added more of the forms that show on the page. Hope this helps. – Yadigit Apr 28 '15 at 02:30

1 Answers1

0

After messing around with the layout of the code, I got it to work, still using

if(isset($_POST["input"]) &&.... {dofunction();}

Thank you Rasclatt for the tip. (fixing how it's processed.)

Instead of having a function store all the if(isset.. I placed each if(isset under each form it belongs to. Everything seems to be working correctly.

Yadigit
  • 41
  • 2
  • 7