如果表格处理条件

时间:2016-08-31 22:47:26

标签: php forms

$credit = $_POST['credits'];
$twitter_impressions = '2';
$twitter_spend='1';
$fb_impressions = '2';
$fb_spend='1';

if (isset($_POST['type'])) {
    if ($_POST['type'] == 'twitter') {
        $total = ($credit * $twitter_spend);
        $convert = (($credit * $twitter_impressions) / $twitter_spend);
        $stmt=$db->prepare("UPDATE social_posts 
                            SET credits = credits + :credits, message = :message 
                            WHERE username = :user AND id = :id");
        $stmt->bindParam(':user', $username);
        $stmt->bindParam(':credits', $convert);
        $stmt->bindParam(':id', $_POST['id']);
        $stmt->bindParam(':message', $_POST['text']);
        $stmt->execute();
    }   

    if ($_POST['type'] == 'facebook') {
        $total = ($credit * $fb_spend);
        $covert = (($credit * $fb_impressions) / $fb_spend);
        $stmt=$db->prepare("UPDATE social_posts 
                            SET credits = credits + :credits, message = :message 
                            WHERE username = :user AND id = :id");
        $stmt->bindParam(':user', $username);
        $stmt->bindParam(':credits', $convert);
        $stmt->bindParam(':id', $_POST['id']);
        $stmt->bindParam(':message', $_POST['text']);
        $stmt->execute();
    }  
}

表单处理仅在$ _POST ['类型']为twitter时才有效,当它是facebook时它不处理。

foreach ($row AS $row) {
?>
    <div class="contain" style="display:none;">
        <div class="alert alert-success" id="<? echo $row['id']; ?>">   
            <form style="height:42px;">             
                <h4 class="pull-left" style="text-transform: capitalize; width:11%;">
                    <? echo $row['type']; ?> Post:
                </h4>
                <textarea class="pull-left" style="margin-right:1%;">
                    <? echo $row['message']; ?>
                </textarea>
                <h4  class="pull-left" style="text-transform: capitalize; width:6%;">
                    Credits:
                </h4>
                <input class="pull-left ucredits" style="width:8%;  margin-right:1%; "
                type="text" value="" />
                <h4  class="pull-left" style="text-transform: capitalize; width:9%;">
                    Impressions:
                </h4>
                <i class="<? echo $row['type']; ?>" style="display:none;">
                    <? echo $row['type']; ?>
                </i>
                <input class="pull-left credits" style="width:9%;  margin-right:1%; " 
                type="text" value="<? echo $row['credits']; ?>" />
                <button class="pull-left btn btn-primary update" 
                id="update<? echo $i++; ?>" value="<? echo $row['id']; ?>" type="submit" 
                style="width:7%; margin-right:1%;" name="credits">Submit</button>               
            </form>
        </div>
    </div>
<? } ?>    

<script>
    $(document).ready(function(){
        $('.alert').on('click', 'button[id^=update]', function(e) {
         e.preventDefault();
         e.stopPropagation();
             var form = $(this).closest( "form" );
             var checkValues = form.find(".update").val();
             var checkCred = form.find(".ucredits").val();
             var checkPost = form.find("textarea").text();
             var checkType = form.find("i").text();
            $.ajax({
                type: "POST",
                url: "update_social_cred.php",
                data: { 
                    id: checkValues, 
                    credits: checkCred, 
                    text: checkPost,
                    type: checkType  
                },
                success:function(result) {
                    alert (checkType);
                }
            });
        });
    });
</script>

表单绝对是提交的,因为我都正确地回显了类型,当类型是twitter时,表单正确处理。但是当类型是facebook时,它不处理。我也尝试将facebook从第3个if语句切换到第2个,但仍然只有在类型是twitter时才正确处理表单。同样,我试图将if更改为if elseif else语句,不做任何更改。

同样为了记录,还有其他类型的我没有在这里显示,除了twitter之外都失败,所有都使用完全相同的格式并且只编码不同的变量。

0 个答案:

没有答案