单击html表单执行php功能

时间:2015-04-27 23:15:43

标签: javascript php jquery html forms

我一直在寻找我的问题的答案。 许多其他人提出了同样的问题,但没有得到他们问题的答案。问题是这个。

我有以下代码。

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();}
}
}

上面的代码工作正常。但是当我开始向同一页面添加更多表单时,我遇到了一些问题,例如上面列出的那个。我的问题是,如果我点击Follow(或取消关注)按钮,所有其他表格也将提交。

我意识到我的问题在哪里。

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

注意:我的所有其他表单都设置了值,例如上面列出的值。 因为它们都已设置,并且它们都有值,即使我没有点击它们,它们也会调用php函数。

经过研究,onclick我试图把这个函数放在表单onclick动作中..例如..

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

但这没有按计划运作。它会在页面加载时执行该功能。所以我开始寻找我的问题的答案,并遇到了一个例子,如..

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

但那不起作用..所以,我在这里寻求帮助。

在案件中,问题不够清楚,或者我有点偏离。这是再次。

我有一个HTML表单。当我点击“提交”时,我需要执行一个php文件。

注意:“if(isset ...”不会这样做。

提前谢谢!

编辑:

以下是所有表单的格式化方式。

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');

我添加

do_action('post_options');

到页面底部或需要的功能..这是一个例子。

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');
}
?>

我想用ajax ..但没有运气。我要求一个简单的ajax示例,显示一个带有2个隐藏值的表单,单击时调用php函数。

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>';
    }
}
}

这只是其中的一小部分。

1 个答案:

答案 0 :(得分:0)

在搞乱了代码的布局后,我让它继续工作,仍在使用

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

谢谢Rasclatt的提示。 (修复它是如何处理的。)

而不是有一个函数存储所有的if(isset ..我放置了每个if(在它所属的每个表单下面都是。所有东西似乎都正常工作。