title: jQuery not recognizing PHP variable under if Statement
body: <p>I have a code which adds to cart using jQuery and php session.</p>
  <p>Everything works fine except from display the status of the action of adding to cart. Such as &quot;Added to cart&quot; , &quot;updated&quot; etc.</p>
  <p>The message is stored in the PHP variable called $status, and after Adding to cart, it's suppose to alert with jQuery saying item added successfully.</p>
  <p>However I tried to alert another php variable outside of an if Statement and it works.. but the variable $status within the if Statements which adds item to cart doesn't display..
  Here is my php code</p>
  <pre><code>&lt;?php
  
  
      if (isset($_POST['pro_id']) &amp;&amp; $_POST['pro_id']!=&quot;&quot;){
    
      // declares variables
      
      if(!is_numeric($_POST['quantity']) || strval(intval($_POST[&quot;quantity&quot;])) != strval($_POST[&quot;quantity&quot;])) {
  
      $error = &quot;Quantity must be interger&quot;;
  
  } else {
      
      $pro_id = $_POST['pro_id'];
      $quantity = $_POST['quantity'];
      $option = $_POST['option'];
     
      // selects product details
      
      $sql = &quot;SELECT * FROM products WHERE pro_id = ?&quot;;
              
      $stmt = mysqli_prepare($link, $sql);
      
      // Bind variables to the prepared statement as parameters
      
                  mysqli_stmt_bind_param($stmt, &quot;i&quot;, $param_id);
                  
      // Set parameters
      
      $param_id = $pro_id;
                  
      mysqli_stmt_execute($stmt);
                      
      /* get result */
                      
      $result = mysqli_stmt_get_result($stmt);
      
      
      // fetches result as assoc array
                      
      
      $row = mysqli_fetch_assoc($result);
      
      // store fetched results in variables
      
      $name = $row['pro_name'];
      $price = $row['pro_price'];
      $option = $_POST['option'];
      
      // selects product image from images table
      
      $sql_img = &quot;SELECT * FROM pro_images WHERE pro_id= ? LIMIT ?&quot;;
      
      $stmt_img = mysqli_prepare($link, $sql_img);
      
      // bind parameters
      mysqli_stmt_bind_param($stmt_img, &quot;ii&quot; ,$param_pro_id, $param_limit);
      
      $param_pro_id = $pro_id;
      $param_limit = 1;
       
      mysqli_stmt_execute($stmt_img);
      
      $img_results = mysqli_stmt_get_result($stmt_img);
      
      $image_re = mysqli_fetch_assoc($img_results); // fetches image an array
      
      // stores fetched image in a variable
      
      $image = $image_re[&quot;pro_image&quot;];
      
      // declares the fetched result into an array to store in session
       
      $cartArray = array(
       $name=&gt;array(
       'name'=&gt;$name,
       'pro_id'=&gt;$pro_id,
       'price'=&gt;$price,
       'quantity'=&gt;$quantity,
       'option'=&gt;$option,
       'image'=&gt;$image)
      ); 
      
      
      // insert cart data into session for guest user into session
      
      if (isset($_SESSION[&quot;cart&quot;]) &amp;&amp; is_array($_SESSION[&quot;cart&quot;])) {
      
      $array_keys = array_keys($_SESSION[&quot;cart&quot;]);
      
      
      // checks if product already exists in cart and delivers message
      
                  if(in_array($name, $array_keys)) {
                      
              $status = &quot;Product updated in cart&quot;;
      
                  } else {
                      
      // Product is not in cart so add it
      
              $_SESSION[&quot;cart&quot;] = array_merge($_SESSION[&quot;cart&quot;], $cartArray);
      
              $status = &quot;Product added to cart&quot;;
      
      
                  }
              } else {
                  
      // There are no products in cart, this will add the first product to cart
                
      $_SESSION[&quot;cart&quot;] = $cartArray;
      
      $status = &quot;Product added to cart&quot;;
      
      }
      }
      
      
      }
      
    
      
      
      
      ?&gt;
  </code></pre>
  <p>And here is my jQuery code</p>
  <pre><code>$(document).ready(function() {
          $('.add').click(function() {
              var pro_id = $(this).attr(&quot;id&quot;);
              var quantity = 1;
              
                  $.ajax({
                      type: &quot;POST&quot;,
                      url: &quot;index.php&quot;,
                      data: ({
                          pro_id: pro_id,
                          quantity: quantity
                      }),
                      cache: false,
                      success: function() {
                      
     alert(&quot;&lt;?php echo $status; ?&gt;&quot;);
                      }
                  });
          });
      });
  </code></pre>
  <p>PS: Both script are on the same page .</p>