0

I have a question: How to get value form Ajax and sent value to PHP ? Can someone help me! Value percentold ajax sent to $percent for option value.

<div class="form-group p-1 m-0">
    <h6>Percent&nbsp;</h6>
    <select class="form-control form-control-lg p-2" name="percent" id="percent" >
<?php
    $percent = 'val(data.percent)'; // <<<--- HERE HOW TO GET Value FORM Ajax and sent here
    echo '<option class="text-white">'.$percent.'</option>';
    for ($i=100; $i>=1; $i--) {
?>  
        <option value="<?php echo $i;?>"><?php echo $i;?>%</option>     
<?php
    }
?>
    </select>
</div>

//////////////////////////////AJAX///////////////////////////

$("body").on("click",".editBtn", function(e){
    e.preventDefault();

    edit_skill = $(this).attr('id');

    //console.log(edit_skill);
    $.ajax({
        url: 'assets/php/process.php', 
        method: 'post',
        data: {edit_skill: edit_skill},
        success:function(response){
            //console.log(response);
            data = JSON.parse(response);
            //console.log(data);
            $("#id").val(data.id);
            $("#title").val(data.skill);
            $("#percentold").val(data.percent);
            $("#sequenceold").val(data.sequence);
            $("#percentold").val(data.percent); <------Neet post go to php 
        }
    });                     
});
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • 2
    It's not clear what you're asking. The code shown already appears to send a value to the server and receive data back from the server. What specifically isn't working? Why do you need to send `data.percent` to the server when you just got that value *from* the server? – David Aug 26 '20 at 11:31
  • $_POST['percentold'] – Dieter Kräutl Aug 26 '20 at 11:36
  • $_POST['percentold'] is show Undefined index: percentold – Atthaphan J_ Aug 26 '20 at 11:41
  • @AtthaphanJ_: That error is discussed at length [here](https://stackoverflow.com/q/4261133/328193). Always search the error message on your favorite search engine first. Aside from that, I don't see anywhere in the code that you're *sending* a value called "percentold" to the server, or where you're trying to read it on the server to produce that error. If you're asking about some other code that you aren't showing us, we can't help with that. – David Aug 26 '20 at 11:55
  • `data: {edit_skill: edit_skill}` – this determines what data you send with your AJAX request. If you want to send an additional parameter, then you need to put it in there. And this is really stuff you should be learning from a beginner tutorial, rather than asking us. – CBroe Aug 26 '20 at 12:04

1 Answers1

0

what is val(data.percent). As is, its just string.

if you want to add it as option from javascript to select then you need to use

<script>
 $('#percent').append(new Option(data.percent, ""));
</script>

in your case :

 $('#percent').append(new Option(data.percent, ""));
flakerimi
  • 2,580
  • 3
  • 29
  • 49