How to get the variable id of a form in ajax

0

I need to know how to send 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 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 comments, but when I add it through ajax, the button stays with the previous amount. I have no idea if you have upgrade.

<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;
           }
       }); 
   });
});
    
asked by anonymous 04.11.2018 / 23:46

1 answer

0

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

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

Your whole code will look 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;
           }
       }); 
   });
});
    
05.11.2018 / 01:59