skip to Main Content

I need to have the response from php update the html form element by id. I’m using Ajax to submit the form, and upon success it needs to change the value of the html element by ID. How can I make this work? Because right now it does nothing!

$.ajax({
    url:"autosave.php?save=true",
    method:"POST",
    data:{"subject":subject,"body":body},
    dataType:"text",
    success:function(data)
         {
         // $('#id').value = this.responseText;
         document.getElementsByName('id')[0].value = this.responseText;
         $('#autoSave').text("Updated");
         }
    });

2

Answers


  1. Chosen as BEST ANSWER

    Working Code:

     $.ajax({
             url:"autosave.php?save=true",
             method:"POST",
             data:{"subject":subject,"body":body},
             dataType:"text",
             success:function(response)
                     {
                      $('#id').val(response);
                      console.log(response);   
                      $('#autoSave').text("Updated");
                      }
    });
    

  2. You should try:

    $.ajax({
        url:"autosave.php?save=true",
        method:"POST",
        data:{"subject":subject,"body":body},
        dataType:"text",
        success:function(data)
             {
             // $('#id').value = this.responseText;         
             jQuery('#id').val( data );
             $('#autoSave').text("Updated");
             }
        });
    

    (Not tested)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search