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

Form processing is only working when $_POST['type'] is twitter, when it is facebook it is not processing.

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>

The form is absolutely submitting because I both have it echoing the type correctly, and when the type is twitter, the form is correctly processing. However when the type is facebook, it is not processing. I have also tried switching facebook from the 3rd if statement to the 2nd, yet still only if type is twitter is the form being correctly processed. Equally I have tried to change the if's to if elseif else statements, no change.

Also for the record, there are other type's I am not showing here, all fail except twitter, all use the exact same format and code only different variables.

Bruce
  • 1,039
  • 1
  • 9
  • 31
  • 1
    What does this `echo "===" . $_POST['type'] . "==="` prints? Is there any trailing spaces left or right in any case (face/twitter)? When you submit – Jorge Campos Aug 31 '16 at 22:54
  • you're incrementing `$i++`, but where is `$i` initialized? – bdereta Aug 31 '16 at 22:56
  • 3
    (Typo: $covert is not $convert.) – Progrock Aug 31 '16 at 22:57
  • ^ error reporting would have been your friend. Undefined variable notice. – Funk Forty Niner Aug 31 '16 at 22:58
  • possible duplicate of [PHP: “Notice: Undefined variable” and “Notice: Undefined index”](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Funk Forty Niner Aug 31 '16 at 22:58
  • @Fred-ii- its weird because when I directly submit my forms (using action=) I get those kind of errors in errors.log, however when I submit via ajax those errors do not show up. – Bruce Aug 31 '16 at 23:02
  • When you POST without Ajax there is no form field with the name of type. – Progrock Aug 31 '16 at 23:08
  • @Progrock what I meant was, when I submit forms using action="" and a submit button, I get errors in my error.log that say things like "undefined variable" - this is a general rule whenever I submit any forms. However when I submit via ajax, those same errors do not appear in my error.log even when present. – Bruce Aug 31 '16 at 23:15
  • You have just one form element with a name. The update button. And hence when submitting without Ajax, you will only have one POST key. You could add a name key to the textarea and ucredits input, and swap out other elements for form elements. Start with a bog standard form/s, and then embellish with Ajax. You can use a hidden field for your type (poor name choice!). – Progrock Aug 31 '16 at 23:20
  • Understanding, but shouldn't it still be able to tell me something like $convert is an undefined variable - as thats literally a variable on the form processing page. Thats what I mean. When submitting the form normally, that error would have popped up telling me an undefined variable - because I submit with ajax, such errors are not reported. – Bruce Aug 31 '16 at 23:30
  • @Bruce you are probably not seeing errors, do you check the Ajax response? Turn on error reporting and tail the logs if you are not displaying the errors inline. – Progrock Aug 31 '16 at 23:38

0 Answers0