Note that there are some explanatory texts on larger screens.

plurals
  1. POPage refreshes on submit click with ajax
    primarykey
    data
    text
    <p>I have a small commenting system that I have modified and try implement into the site. It's in 'ajax'. When the jQuery with HTML is embedded into the page the commenting system works fine - i.e. when the user clicks on a submit button the code returns 'false', stops the page from refreshing and submits data. BUT when I implemented the code within my site and placed it in a seperate .js file the code for some reason doesn't work properly. I mean - the page after the onclick refreshes. Why is that so ? The code is not changed at all - when on its own, it works but not in the index.php site when implemented. I tried to change input type to 'button' and call a function from onclick - the page doesn't refresh but also doesn't insert the input..I'm running out of ideas as to why it is that so. Here's the code:</p> <pre><code>$(document).ready(function () { $(".submit").click(function () { var name = $("#name").val(); var email = $("#email").val(); var comment_area = $("#comment_area").val(); var dataString = 'name=' + name + '&amp;email=' + email + '&amp;comment_area=' + comment_area; var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; var emailaddressVal = $("#email").val(); if (name == '' || !emailReg.test(emailaddressVal) || comment == '') { alert('Please enter valid data and type in message'); return false; } else { $.ajax({ type: "POST", url: "comments.php", data: dataString, cache: false, success: function (html) { $("#com_list").append(html); $("#com_list").fadeIn("slow"); $("#flash").fadeOut('fast'); } }); } return false; }); }); //END //COM LIST //HTML / PHP &lt;div class="slider"&gt; &lt;form id="comment_form" name="comment_form" method="post" action="#" enctype="multipart/form-data"&gt; &lt;input type="text" id="name" name="name" maxlength="16"/&gt;&amp;nbsp;Name&lt;br /&gt;&lt;br/&gt; &lt;input type="text" id="email" name="email"/&gt;&amp;nbsp;Email&amp;nbsp;(will not show)&lt;br /&gt;&lt;br/&gt; &lt;textarea id="comment_area" name="comment_area" maxlength="1000"&gt;&lt;/textarea&gt;&lt;br /&gt;&lt;br/&gt; &lt;input type="submit" class="submit" name="submit_comment" value="submit"/&gt;&amp;nbsp;&amp; nbsp;comment or &lt;a href="index.php" id="cancel" onmousedown="$('.slider').hide();$('#com_list').show();"&gt;&lt;u&gt;cancel&lt;/u&gt;&lt;/a&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p>//comments.php</p> <pre><code> if($_POST) { $name=$_POST['name']; $email=$_POST['email']; $comment_area=$_POST['comment_area']; //$lowercase = strtolower($email); //$image = md5( $lowercase ); $insert = mysqli_query($connect,"INSERT INTO comments (name,email,comment,com_date) VALUES ('$name','$email','$comment_area',curdate())"); } </code></pre> <p>////////////////</p> <p>Thanks for any suggestions..</p>
    singulars
    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.
 

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