PHP ajax not updating database

时间:2017-11-26 12:37:56

标签: php jquery ajax

I'm currently trying to update my database with PHP and AJAX but this doesn't seem to work and i can't currently locate where the error is from

Here are my codes

php

<?php 
  $userid = $_GET['userid'];
  $_SESSION['userid'] = $userid;
  $sql = $db->prepare("SELECT * FROM users WHERE userid = ?");
  $sql->bindParam(1, $userid);
  $result = $sql->execute();
  while ($row = $result->fetchArray(SQLITE3_ASSOC)) 
  {
      $fname = $row['fname'];
      $lname = $row['lname'];
      $cname = $row['cname'];
      $crcnum = $row['crcnum'];
      $uemail = $row['uemail'];
      $uname = $row['uname'];
      $regas = $row['regas'];
      $caddress = $row['caddress'];
      $uaddress = $row['uaddress'];
      $package = $row['package'];
      $regdate = $row['regdate'];
      $expdate = $row['expdate'];
      $profimages = $row['profimages'];
      $coverimage = $row['coverimage'];
      ?>
      <style type="text/css">
        .loginDanger {
            width: 100%;
        }
      </style>
      <div class="upgrForm">
            <div class="logresult"></div>
            <form action="upgformexec.php" method="post" enctype="multipart/form-data">
            <?php 

            echo "<p class='pcLabel'>Current Package</p>";

            if ($package == "Gold")
            {
                echo "<p class='goldpacks'>$package</p>
                <p class='pcLabel'>This user is already on the highest package!.</p>";
            }
            if ($package == "Silver")
            {
                echo "<p class='silvpacks'>$package</p>
                <p class='pcLabel'>Ugrade Pack To</p>
            <select name='newPack' id='newPack'>
                <option>--Select--</option>
                <option>Gold</option>
            </select>
            <input type='submit' name='upgrdSub' id='upgrdSub' value='Execute Upgrade'>";
            }
            if ($package == "Bronze")
            {
                echo "<p class='bronzpacks'>$package</p>
                <p class='pcLabel'>Ugrade Pack To</p>
            <select name='newPack' id='newPack'>
                <option>--Select--</option>
                <option>Silver</option>
                <option>Gold</option>
            </select>
            <input type='submit' name='upgrdSub' id='upgrdSub' value='Execute Upgrade'>";
            }
            if ($package == "Free Membership")
            {
                echo "<p class='freepacks'>$package</p>
                <p class='pcLabel'>Ugrade Pack To</p>
            <select name='newPack' id='newPack'>
                <option>--Select--</option>
                <option>Bronze</option>
                <option>Silver</option>
                <option>Gold</option>
            </select>
            <input type='submit' name='upgrdSub' id='upgrdSub' value='Execute Upgrade'>";
            }
            ?>       
</form>

Below is code to execute the form

<?php 
session_start();
require_once ("db.php");
$db = new MyDb();

$userid = $_SESSION['userid'];

if (isset($_POST['upgrdSub']))
{
    $newpack = strip_tags(@$_POST['newPack']);

    $sql = $db->prepare("UPDATE users SET package = ? WHERE userid = ?");
    $sql->bindParam(1, $newpack, SQLITE3_TEXT);
    $sql->bindParam(2, $userid);
    $result = $sql->execute();

    if ($result) 
    {
        echo "true";
    }
    else
    {
        echo "false";
    }
}
?>

Below is the ajax request

$(document).ready(function() {
    $("#upgrdSub").click(function() {
      //e.preventDefault();
      newpack=$("#newPack").val();
      $.ajax({
        type: "POST",
        url: "upgformexec.php",
        data: "newPack="+newpack,
        success: function(html){
          if (html == 'true')
          {
              $(".logresult").html('<div class="loginSuccess"><i class="fa fa-tick"></i>  Upgrade Successful.</div>');
          }
          else
          {
              $(".logresult").html('<div class="loginDanger"><i class="fa fa-exclamation-triangle"></i>  Please select a package to upgrade to!.</div>');
          }
          if (html == 'false')
          {
              $(".logresult").html('<div class="loginDanger"><i class="fa fa-exclamation-triangle"></i> There was an error upgrading this account!. Please try again later.</div>');
          }
        },
        beforeSend:function()
        {
          $(".logresult").html("<img src='images/ring.gif'>");
        }
      });
      return false;
    }); 
});

For some reason, the form submits without the ajax request but when i had the ajax, i get the "Please select package to upgrade to" feedback. How can this be resolved? Thanks

1 个答案:

答案 0 :(得分:1)

删除此行

callsendAPI(sender_psid,response1);
callsendAPI(sender_psid,response2);

按钮值不会通过if (isset($_POST['upgrdSub'])) { 请求发送。您只能发送ajax newPack其他内容。

而是这样做:

key => value