Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda