Viewed   74 times

I can't get the following PHP + jQuery to work - all I want the script to do is pass the value through ajax, and get the php to grab it, check it matches and add 1 to score.

This is the code I've written:

<?php
$score = "1";

$userAnswer = $_POST['name'];

if ($_POST['name'] == "145"){
    $score++;
}else{
    //Do nothing
}

echo $score;

?>


<script type="text/javascript">

$(document).ready(function() {

    $("#raaagh").click(function(){

    var value = "145";

    alert(value);

    $.ajax({
        url: 'processing.php', //This is the current doc
        type: "POST",
        data: ({name: value}),
        success: function(){
            location.reload();
       }
    });


    });
});

</script>

<p id="raaagh">Ajax Away</p>

Thanks for the help, I've changed GET to POST in both instances, and no joy - there's something else wrong.

 Answers

3

First of all: Do not go back to the dark ages... don't use the same script to generate HTML and to respond to an ajax request.

I can't make any sense of what you are trying to do... Let me change your code so it at least makes some sense and document what's going on. It seems like the problem is the fact that you are calling location.reload from your success handler.

// ajax.php - Outputs 2 if the name parameter is 145, 1 otherwise (????)

<?php
$score = "1";    
$userAnswer = $_POST['name'];    
if ($_POST['name'] == "145"){
    $score++;
}       
echo $score;    
?>

// test.html

<script type="text/javascript">  
$(document).ready(function() {    
    $("#raaagh").click(function(){    
        $.ajax({
            url: 'ajax.php', //This is the current doc
            type: "POST",
            data: ({name: 145}),
            success: function(data){
                // Why were you reloading the page? This is probably your bug
                // location.reload();

                // Replace the content of the clicked paragraph
                // with the result from the ajax call
                $("#raaagh").html(data);
            }
        });        
    });
});

</script>

<p id="raaagh">Ajax Away</p>
Friday, October 21, 2022
 
borzole
 
4

Firstly make ajax to separate PHP page where you will access the radio value. Also make alert after you receive the data.

$.ajax({
    url : "post.php",
    type : "POST",
    data: pass_data,
    success : function(data) {
        // alert radio value here
        alert(data);
    }
});

Crete a separate PHP file post.php where you access radio input. Since you are making POST request you need to use $_POST instead of $_GET to get radio button value.

<?php 
    $radio1 = $_POST['radio1'];
    echo $radio1;
?>
Wednesday, November 23, 2022
 
4

I think you've got almost everything. The callback function you have under success needs an argument which stands for the results from search.php

     success: function(res) {
             goToByScroll("result");
             $('#result').html("<br><br><br><br><br><br><br><br><br><br><div class='center'><img src='img/loader.gif' /></div>").hide().fadeIn(2500, function() {
                 $('#result').html(res + "<br /><br /> Finished");
             });
         } 

res is everything outputted be search.php. Echo, stuff outside of php tags, etc Anything you'd see if you loaded search.php itself.

I don't know if you wanted 'Finished' to still be there. Take it out if you dont.

Tuesday, October 18, 2022
 
1

Try uploading the file as form data

audioRecorder.exportWAV(function(blob) {

      var url = (window.URL || window.webkitURL).createObjectURL(blob);
      console.log(url);

      var filename = <?php echo $filename;?>;
      var data = new FormData();
      data.append('file', blob);

      $.ajax({
        url :  "lib/vocal_render.php",
        type: 'POST',
        data: data,
        contentType: false,
        processData: false,
        success: function(data) {
          alert("boa!");
        },    
        error: function() {
          alert("not so boa!");
        }
      });
}); 

.

<?php 

if(isset($_FILES['file']) and !$_FILES['file']['error']){
    $fname = "11" . ".wav";

    move_uploaded_file($_FILES['file']['tmp_name'], "../ext/wav/testes/" . $fname);
}
?>
Saturday, August 6, 2022
3

i think your problem may be in ajax code since you are using formData object . try append the message variable with it

$('#submit').on('click', function(){

  var fd = new FormData(this);
  fd.append('file',$('#file')[0].files[0]);
  fd.append('message ',$('#message').val());

  $.ajax({
    method:"POST",
    url:"<?php echo site_url('home/send_chat');?>",    
    data: fd,  
    cache: false,
    contentType: false,
    processData: false,   
    success: function(data){                 
      alert(data);
    },
    error: function(xhr, status, error) {
      alert(xhr.responseText);
    }  
  });
});
Saturday, November 12, 2022
 
malina
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :