Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't use if statement to check for POST data.
    primarykey
    data
    text
    <p>I am using ajax call to grab some data from my mysql database. This is my ajax call: </p> <pre><code> $.ajax({ type: "POST", url: "fetch.php", data: id, success: function(msg) { var record = JSON.parse(msg); $("#name").val(record.name); $("#description").val(record.description); switch(record.category) { case ("Residential"): $("#category").val("residential"); break; case ("Modernisation &amp; Domestic Extensions"): $("#category").val("modernisation"); break; case ("Feasibility Layouts"): $("#category").val("feasibility"); break; case ("Master Planning"): $("category").val("master"); break; default: $("category").val(""); }; switch(record.featured) { case ("y"): $("#featured").val("y"); break; default: $("featured").val("n"); }; } }) </code></pre> <p>and this is my php file:</p> <pre><code> &lt;?php $dbc = mysqli_connect('XX','XX','XX','XX'); $id = $_POST['id']; if($dbc) { $row = fetchDataFromRecordWithId($dbc,$id); } else { echo 'Database error'; } function fetchDataFromRecordWithId($dbc,$id) { $q = "SELECT * FROM Projects WHERE id = ".$id; $r = mysqli_query($dbc, $q); $row = mysqli_fetch_array($r, MYSQLI_ASSOC); mysqli_close($dbc); return $row; } echo json_encode($row); ?&gt; </code></pre> <p>Everything work flawless but soon when I try to add if statement in my php file to check if there is any POST data than my ajax call is not getting any response. I tried different methods :</p> <pre><code> &lt;?php if(isset($_POST)) { .. my whole php code here .. } ?&gt; &lt;?php if(!empty($_POST)) { ..my whole php code here .. } ?&gt; &lt;?php if($_POST) { .. my whole php code here .. } ?&gt; &lt;?php if($_POST['id']) { .. my whole php code here .. } ?&gt; </code></pre> <p>but nothing work! Ajax call is not getting json data back when I will use any of these if statements listed above. </p> <p>I could not check for POST data and leave it like it is but I want to do is in proper way.</p> <p>It seems like if statement is not executed (but POST data must be there as I am able to fetch <code>$_POST['id']</code> value when if statement not used). </p> <p>I also tried to put <code>echo json_encode($row)</code> outside if statement but it didn't help either. Any ideas what is wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload