j.Doe j.Doe - 7 months ago 31
Javascript Question

how do i $_get with ajax

im sorry im still new with ajax.i have a problem getting a value with ajax.
i have a set of question from database.i loop them along with the edit button in a table.i can do the $_get fine if i do

<a href=somepage.php?qid= echo $row['qid']>link</a>


but what i really want to do is edit on the same page without reloading the page.

$('.inline.positive.ui.icon.button.edit').click(function(e) {
$.ajax({
url : 'view/addquestion.php',
type : 'GET',
data : {
'qid' : $row['question_id'],
},
success : function(data) {
alert("Hi, testing");
}
});
});


this is my ajax[updated]

i send the value with

while ($row = mysqli_fetch_array($question)) {
<div class="ui buttons">
<button class="inline positive ui icon button edit" data-qid='<?php $row["question_id"]?>'>
<i class="edit icon"></i>
</button>
}


i tried to get the question_id for where clause to update that specific question in database.with

if (isset($_GET["qid"]))
{
$qid = $_GET["qid"];
}
else
{
$qid = 0;
}


if it helps.im currently using

<script src="https://code.jquery.com/jquery-2.2.3.js" integrity="sha256-laXWtGydpwqJ8JA+X9x2miwmaiKhn8tVmOVEigRNtP4=" crossorigin="anonymous"></script>


thanks in advance




Latest Update :

so i think i get ajax and php abit now.
reason why i cant get the $_GET['qid'] is because the php script is run in server-side which is why i keep getting $_GET['qid'] = 0 as the page did not refresh.(please correct me if im wrong)

as for the ajax.. i update it with new script

$(document).ready(function(){
$(".inline.positive.ui.icon.button.edit").click(function() {
var val = $(this).attr("data-qid");
$.ajax ({
url: "view/addquestion.php",
data: { val : val },
success: function( data ) {
alert("Hi, testing" + val);
}
});
});
});


the ajax is working now and the alert came out with the correct value.

thanks to apokrypos and nadir(i dont know how to tag people) for helping me with this.

on a side note and a question that is unrelated with this one.
how do i echo? or print javascript variable in input field thats already exist?

Answer

Note the differences between server-side and client-side scripting. All server side scripts are executed and finished before any JavaScript is loaded. You can't access any PHP variables from JavaScript simply because PHP is not running on the same place JavaScript is. You'll have to pass the variables through your HTML to the client side where JavaScript can access them.

Example:

PHP Loop

<?php while ($row = mysqli_fetch_array($question)): ?>
  <div class="ui buttons">
      <button  class="inline positive ui icon button edit" data-qid='<?= $row["question_id"]?>'> 
         <i class="edit icon"></i>
       </button>
<?php endwhile; ?>

jQuery Ajax

$('.inline.positive.ui.icon.button.edit[data-qid]').click(function(e) {
  $.ajax({
    url : 'view/addquestion.php',
   type : 'GET', 
   data : {
           'qid': $(this).attr("data-qid"),
          },
           success : function(data) {
           }
     });
  });
Comments