Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery function not working a second time after form submitted
    primarykey
    data
    text
    <p>I am submitting a form via a jqery function that's dynamically created with using php and a while loop. The form gets submitted correctly the first time and I generate html and display it using .html(response). I replicate the code so that the function will work again, however it doesn't. When I submit the form a second time (without a page refresh), the form sends me to the action attribute.</p> <p>Is this a limitation of jquery?</p> <p>My code:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function() { $("#submit&lt;? echo"$counter$counter";?&gt;").click(function() { var action = $("#commentform&lt;? echo "$counter$counter";?&gt;").attr('action'); var form_data = { comment: $("#comment&lt;? echo "$counter$counter";?&gt;").val(), type: $("#commenttype&lt;? echo "$counter$counter";?&gt;").val(), typeid: $("#commenttypeid&lt;? echo "$counter$counter";?&gt;").val(), counter: $("#commentcounter&lt;? echo "$counter$counter";?&gt;").val(), is_ajax: 1 }; $.ajax({ type: "POST", url: action, data: form_data, success: function(response) { $("#commentarea&lt;? echo"$counter$counter";?&gt;").html(response); } }); return false; }); }); &lt;/script&gt; </code></pre> <p>My form that's on the same page as the script I am running:</p> <pre><code>&lt;div class="postcommentscomment" style="size:10px; font-family:Verdana, Geneva, sans-serif;"&gt; &lt;form action="php/comment.php" id="commentform&lt;? echo "$counter$counter";?&gt;" method="post" name="comment" target="_self"&gt; &lt;textarea id="comment&lt;? echo "$counter$counter";?&gt;" style="width:162px; height:24px; color:#827e78; font-size:10px;"&gt;comment here&lt;/textarea&gt; &lt;div style="float:right; position:relative; margin-top:5px;"&gt; &lt;input id="commenttype&lt;? echo "$counter$counter";?&gt;" type="hidden" value="&lt;? echo $type;?&gt;" /&gt; &lt;input id="commenttypeid&lt;? echo "$counter$counter";?&gt;" type="hidden" value="&lt;? echo $id;?&gt;" /&gt; &lt;input id="commentcounter&lt;? echo "$counter$counter";?&gt;" type="hidden" value="&lt;? echo "$counter$counter";?&gt;" /&gt; &lt;input id="submit&lt;? echo"$counter$counter";?&gt;" type="button" value="comment"/&gt; &lt;/div&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p>My form on the html(response) page via the action attribute</p> <pre><code>&lt;div class="postcommentscomment" style="size:10px; font-family:Verdana, Geneva, sans-serif;"&gt; &lt;form action="php/comment.php" id="commentform&lt;? echo "$counter";?&gt;" method="post" name="comment" target="_self"&gt; &lt;textarea id="comment&lt;? echo "$counter";?&gt;" style="width:162px; height:24px; color:#827e78; font-size:10px;"&gt;comment here&lt;/textarea&gt; &lt;div style="float:right; position:relative; margin-top:5px;"&gt; &lt;input id="commenttype&lt;? echo "$counter";?&gt;" type="hidden" value="&lt;? echo $type;?&gt;" /&gt; &lt;input id="commenttypeid&lt;? echo "$counter";?&gt;" type="hidden" value="&lt;? echo $id;?&gt;" /&gt; &lt;input id="commentcounter&lt;? echo "$counter";?&gt;" type="hidden" value="&lt;? echo "$counter";?&gt;" /&gt; &lt;input id="submit&lt;? echo"$counter";?&gt;" type="button" value="comment"/&gt; &lt;/div&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p>NOTE: The counter php variable is set such that $counter on the html(response) page produces an equivalent result as $counter$counter on the original page.</p> <p>So, I submit a comment, the comment gets inserted into the database, then a query is run, a form is reproduced and displayed on the original page via html(response). When I then try to submit a new comment I get redirected to the action page. I stopped that by changing "submit" to "button", but that doesn't give me the result I want -- which is to be able to submit the form again.</p> <p>Not sure if this is a jquery limitation or a problem with my code.</p> <p>Also, the comments are on a page that has may different posts. So multiple comment forms. If there is a better way to execute a ajax refresh of the comments for a specific post, I am open to changing code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload