How to take the variable ID of a form in ajax

Asked

Viewed 112 times

0

I need to know how to submit the form id, which varies according to the post ID, which would be something like myForm'. $id. ' to be able to treat each form itself. The form is sent through the script:

function rpbox(value, valor) {
    $(document).ready(function() {
        $("#"+value).keydown(function(evt) {

            var message = $("#"+value).val();

            if (evt.keyCode == 13 && !evt.shiftKey) {        

                if (message != ''){
                    if (document.getElementById("reply_box"+valor).style.display == "none") {
                        document.getElementById("reply_box"+valor).style.display = "table";
                        document.getElementById("l"+valor).style.display = "none";
                    }
                    $("#myForm"+valor).submit();

                }
                $("textarea").val('');
                evt.preventDefault();
                return false;
            }
         });
    });
}

This script would be to handle the form I need to handle the Iddoform.

$(document).ready(function() { 

    $("#"+IDdoform).ajaxForm({
        target: ".comment_all",
        type: "POST",
        success: function(){
            $(".comment_all").append;
        }
    }); 

});

Another issue is that I have a "button" that shows the amount of existing comments, but when added by ajax, the button gets the previous amount. I have no idea if there’s any way to update him.

<input class="lsubmit4" type="button" name="'.$post['id_p'].'" value="Comment ('.$rows.')" onclick="openReply(this.name)"/>

$(document).ready(function() { 

   function rpbox(value, valor) {
        $("#"+value).keydown(function(evt) {
            var message = $("#"+value).val();
            if (evt.keyCode == 13 && !evt.shiftKey) {        

                if (message != ''){
                    if (document.getElementById("reply_box"+valor).style.display == "none") {
                        document.getElementById("reply_box"+valor).style.display = "table";
                        document.getElementById("l"+valor).style.display = "none";
                    }
                    $("#myForm"+valor).trigger("submit", valor);

                }
                $("textarea").val('');
            }
         });
   }

   $(".imagem_news").click(function(){
      var id = this.id;
      var valor = this.id.match(/\d+/);
      rpbox(id, valor);
   });

   $("form").submit(function(e, i){
	   var myForm = "myForm"+i;
		e.preventDefault();
		$("#"+myForm).ajaxForm({
			type: 'POST',
			url: 'processing.php',
			target: '.comment_all',
			success: function(){
				$(".comment_all").append;
           }
       }); 
   });
});

  • Have you tried sending by data: { idform: IDdoform}?

  • In the case of the button, as it is sent by PHP, I think you should take the updated value after making a query to the bank taking the amount and adding +1, if that’s what I understood.

  • @Sam the date I would have to use inside the ajaxForm, no? I need to receive this Iddoform before calling the ajax. The form id is usually myForm'. $post['id'].'.

  • I get it. In this $("#"+IDdoform).ajaxForm({ you need the variable IDdoform is the form ID.

  • How is called this function rpbox?

  • @Sam <textarea name="editor1" id="text'.$post['id_p'].'" onclick="rpbox(this.id, '.$post['id_p'].')" class="imagem_news" placeholder="Your Comment..." ></textarea>

  • @Sam then, on the button, is there any way to update it without updating the page?

  • You can update the button via Ajax by name: $("[name="+ID+"]").val("Comment ("+VALOR RETORNADO DO AJAX+")")

Show 3 more comments

1 answer

0

Your code has some problems and things kind of meaningless, like a $(document).ready(function() { within the function(?). This one evt.preventDefault(); and return false; have no reason to be either.

What you should do is create an event submit to pick up the submission form and send your id through a parameter in the trigger submit. You also don’t need to use onclick to call the function rpbox. Create an event click for this, so you can put all the code in the scope of the $(document).ready(function() {.

Your code will be like this:

Textarea without onclick

<textarea name="editor1" id="text'.$post['id_p'].'" class="imagem_news" placeholder="Your Comment..." ></textarea>

Script

$(document).ready(function() { 

   function rpbox(value, valor) {
        $("#"+value).keydown(function(evt) {

            var message = $("#"+value).val();

            if (evt.keyCode == 13 && !evt.shiftKey) {        

                if (message != ''){
                    if (document.getElementById("reply_box"+valor).style.display == "none") {
                        document.getElementById("reply_box"+valor).style.display = "table";
                        document.getElementById("l"+valor).style.display = "none";
                    }
                    $("#myForm"+valor).trigger("submit", valor);

                }
                $("textarea").val('');
            }
         });
   }

   $(".imagem_news").click(function(){
      var id = this.id;
      var valor = this.id.match(/\d+/);
      rpbox(id, valor);
   });

   $("form").submit(function(e, i){
      e.preventDefault();

       $("#myForm"+i).ajaxForm({
           target: ".comment_all",
           type: "POST",
           success: function(){
               $(".comment_all").append;
           }
       }); 
   });
});
  • I made some modifications to suit what I need, but what happens is that when I type something, it skips a line and erases what was written and does not go to the processing page. I will edit in the post the modification I made.

  • I’ve been trying to make it work, and the closest I’ve come is ajaxSubmit, but then you input the 2x data. But anyway, the form goes to the Processing.php page and does not return to the post. Another thing is the append function, it deletes the other comments :s And unintentionally, I edited your reply, sorry :/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.