Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
updating a database when clicking on a button

I have a button here:

<button type="button" class="btn btn-primary">add me</button>

I tried to use AJAX; in my database I have a sp_status ( in my $UserData which is an array). I wanted to change the user sp_status in database with id of $UserData['sp_id'] from 5 value to 0. Here is the code I try to do this with it:

<script>
    $(document).ready(function(){
    $(".btn").click(function(){
        alert("PROBLEM IS HERE!");  
        $.ajax({
        url: "update.php",
        type: "POST",
        data: {uid: $UserData['sp_id']}, //this sends the user-id to php as a post variable, in php it can be accessed as $_POST['uid']
        success: function(data){
            data = JSON.parse(data);
        }
    });
});
}); 
</script>

The $UserData array has sp_id as identifier of the user. Inside of update.php in same folder I wrote this code:

<?php
if(isset($_POST['uid'])){
    $query = mysql("UPDATE `signup_participant` SET sp_status = 0 WHERE sp_id = ".$_POST['uid']));
    $results = mysql_fetch_assoc($query);
    echo json_encode($results);
}

There are two main problems I face with. First when I move alert("PROBLEM IS HERE!");one line down, and click, nothing happens.
The other problem is about update.php. I copy and paste UPDATEsignup_participantSET sp_status = 0 WHERE sp_id =10 (static value of 10) and everything works fine in console of phpMyadmin.
I read a lot of questions here on stack, but no one helps. Can anyone fix this?
EDIT: As friends said, I change my code to this ( the AJAX part):

<script>
    $(document).ready(function(){

    $(".btn").click(function(){
        alert('Hello');
        $.ajax({
        url: "update.php",
        type: "POST",
        data: {uid: "<?php echo $UserData['sp_id'];?>"}  
        success: function(data){
        data = JSON.parse(data);
        }
        });
    });
});
</script>

The weird thing is that the popup alert is not shown anymore. Is there any ideas?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Yes, you have to mistakes:

The first one is "$UserData['sp_id']" which you called in a Javascript code, it's not clear what do you mean from this variable or where did it come from, if it's a value of an Html element then it should be:

data: {uid: $('#sp_id').val();},

And if it's a Php variable it would be:

data: {uid: <?php $UserData['sp_id']; ?>},

The next mistake is in the Php server code:

$query = mysql("UPDATE `signup_participant` SET sp_status = 0 WHERE sp_id = ".$_POST['uid']));
$results = mysql_fetch_assoc($query);

Those two commands are separated, the first one is for updating database and the second one for showing results from it and they must be like follow:

$query = "UPDATE `signup_participant` SET sp_status = 0 WHERE sp_id = ".$_POST['uid'];
mysql_query($query);

$query   = "SELECT * from `signup_participant` WHERE sp_id = ".$_POST['uid'];
$select  = mysql_query($query);
$results = mysql_fetch_assoc($select);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!