简体   繁体   中英

PHP ajax not updating database

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

Remove this line

if (isset($_POST['upgrdSub']))
{

The button value is not sent via ajax request. You only send the newPack key => value nothing else.

Instead do this:

if (isset($_POST['newPack']))
{

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM