javascript - html form on click do php function -


this question has answer here:

i've been searching around answer question. many other people have asked same question did not answer question. question this.

i have 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 * followers user = $loguser , 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. ran problems when started adding more forms same page such 1 listed above. problem im having is, if click follow (or unfollow) button, other forms submit.

i realize problem was.

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

note: other forms have set value such 1 listed above. because set, , have value, call php function though did not click on them.

after researching, onclick tried put function inside form onclick action.. eg..

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

but didn't work planned. execute function when page loads. started quest find answer problem , came across example such as..

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

but didn't work.. so, here asking help.

in-case question wasn't clear enough or got little sidetracked. here again.

i have html form. when click "submit" need execute php file.

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

thank in advance!

edited:

the following how forms 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 bottom of page or function needed.. here 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 * wallpost order id desc limit $offset, $limit" ); if( $entries ) { $count = 1; $class = '';     foreach( $entries $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(*) 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 use ajax.. no luck. asking simple ajax example showing form 2 hidden values, when clicked, call 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, 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 * followers user = $loguser , 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 * reported_posts postid = $messageid , 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 * reported_posts postid = $messageid , 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 few of them.

after messing around layout of code, got work, still using

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

thank rasclatt tip. (fixing how it's processed.)

instead of having function store if(isset.. placed each if(isset under each form belongs to. seems working correctly.