6

I am new to JavaScript, so pardon my lack of knowledge.

I am trying to code a contact form and want to get the value entered in a particular input text field as soon as a value entered by the visitor using AJAX and pass it to a PHP string so that I can show relevant information based on the value entered before hitting the submit button.

How Can I do that using jQuery?

Here are the codes I am using for the form:

<form method="post" action="">
<input type="text" id="name" name="name" />
<input type="text" id="email" name="email" />
<input type="text" id="subject" name="subject" />
<input type="text" id="url" name="url" />
<input type="checkbox" id="showdetails" name="showdetails" /> Show URL Details
<input type="submit" value="Submit" />
</form>`

I need the value of the URL input field to pass to a php string (as soon as the user checks the showdetails checkbok) so that I can process it in background to show information based on the url.

Abhik
  • 664
  • 3
  • 22
  • 40
  • 4
    Using what? jQuery? mootools? native JS? Have you tried anything? – Jonnix Jan 07 '13 at 12:37
  • Yes, jQuery. Sorry I didn't mentioned that. Edited my question and added that. – Abhik Jan 07 '13 at 12:49
  • And do you want it _as soon as a value entered by the visitor_ or _(as soon as the user checks the showdetails checkbok)_ ??? There is a huge difference in user experience. – mplungjan Jan 07 '13 at 12:52
  • @mpungjan, as soon as the user checks the checkbox. Then it should check if any value entered in the field and pass it to a php string I could use in my functions. If no value is entered yet the box is checked, an error message pops up. Sorry, I am totally novice in JavaScript, so please help. – Abhik Jan 07 '13 at 13:08

2 Answers2

8

Given this html:

<form method="post" action="">
<input type="text" id="name" name="name" />
<input type="text" id="email" name="email" />
<input type="text" id="subject" name="subject" />
<input type="text" id="url" name="url" />
<input type="checkbox" id="showdetails" name="showdetails" /> Show URL Details
<input type="submit" value="Submit" />
</form>

jQuery (checkbox)

$(function() {
  $("showdetails").on("click",function() {
    var url = $("#url").val();
    if (url && $(this).is(":checked")) {
      $.get("relevantinfo.php",
        { "url": url },
        function(data){
          // do something with data returned
        }
      );
    }
    else {
      // clear relevant info here
    }
  });
});

The receiving end: Retrieving POST Data from AJAX Call to PHP

Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
3

get the input string using onchange event in text field .

ref : https://developer.mozilla.org/en-US/docs/DOM/element.onchange

Then using that string content, call AJAX function.

function load(string) {
    var xmlhttp;
    if(window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "YOUR_URL", true);
    xmlhttp.send();
}

As suggested in the comments, use jquery + AJAX. For jquery,

$.ajax({
    url: 'ajax/test.html',
    success: function(data) {
        $('.divClass').html(data);
        alert('Load was performed.');
    }
});
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Bharath Mg
  • 1,117
  • 1
  • 9
  • 18
  • 1
    Instead of `XMLHttpRequest`, use jquery + Ajax to simplify – iLaYa ツ Jan 07 '13 at 12:45
  • @LearneR jQuery is kinda big if you only use it for AJAX handling. – Robin van Baalen Jan 07 '13 at 12:47
  • I suggest onclick of the checkbox rather than onchange since onchange needs a blur in some browsers – mplungjan Jan 07 '13 at 12:50
  • if jQuery is not already being used including the entire library to do XHR functions is overkill. Especially if dated browser support is not critical ([alternatives](http://microjs.com/#ajax)). I realize the OP asked for it, consider this a side comment. – rlemon Jan 07 '13 at 13:22