Krage Kieswetter Krage Kieswetter - 2 years ago 148
HTML Question

Ajax is not working with submit button in HTML form while using the onclick option

The code below works when i use just the input text field but it doesn't works when i use a submit button. I don't know jquery so please explain

<!DOCTYPE html>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}"POST", "download.php?q="+str, true);

<p><b>Start typing a name in the input field below:</b></p>
<form method="POST" action="showHint(str)">
First name: <input type="text" placeholder="Copy Your Facebook Video URL Over Here" name = "URL" onclick="showHint(URL.value)" >
<button type="submit" onclick="showHint(URL.value)">Download</button>
<p>Suggestions: <span id="txtHint"></span></p>

However when i use the submit button instead it doesn't works Please help

Answer Source

Same thing happened with me instead of using button use input type = button like described below -

<input type="button" id="submit" value="submit" name="submit" onclick="showHint(URL.value)"></input>

it would surely work

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download