0

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

4
  • 1
    ErrorLog & Console.Log are empty? Commented Nov 26, 2017 at 12:38
  • Could that be a reason? Commented Nov 26, 2017 at 12:48
  • Mostly. Maybe an error help to solve your problem Commented Nov 26, 2017 at 12:49
  • Tried, didn't get any errors. Commented Nov 26, 2017 at 13:00

1 Answer 1

1

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']))
{
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks.Works fine now.
my pleasure friend!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.