webgrrrl
07-17-2007, 02:52 AM
I am trying to use ajax to post one form field to a php page. I am having a nightmare so have simplified it to the bare minimum to get something working, but it just does nothing when the button is cicked.
Here is the Javascript/ajax code:
<script type="text/javascript">
var http = createRequestObject();
function createRequestObject() {
var objAjax;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
objAjax = new ActiveXObject("Microsoft.XMLHTTP");
}else{
objAjax = new XMLHttpRequest();
}
return objAjax;
}
function postTags(){
var fmTag=document.getElementById("fm_Tag").value;
http.open('POST','ajaxtest.php', true);
http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
http.send('tag=' + fmTag);
http.onreadystatechange = handleResponse;
}
</script>
the html:
<form>
<label>Enter a Tag</label><input type="text" name="fm_tag" size="20" /> <br><br>
<input type="Button" name="Enter" value="enter" onclick="postTags()"/>
</form>
and finally the ajaxtest.php page
<?
$tag = $_GET["tag"];
?>
<script type="text/javascript">
alert("<?$tag;">;
</script>
I have a GET request working fine using the same createRequestObject(); script - i can't get my head round why something so simple isn't working when loads of code examples use the same script.
Can anyone help please?
:confused:
Here is the Javascript/ajax code:
<script type="text/javascript">
var http = createRequestObject();
function createRequestObject() {
var objAjax;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
objAjax = new ActiveXObject("Microsoft.XMLHTTP");
}else{
objAjax = new XMLHttpRequest();
}
return objAjax;
}
function postTags(){
var fmTag=document.getElementById("fm_Tag").value;
http.open('POST','ajaxtest.php', true);
http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
http.send('tag=' + fmTag);
http.onreadystatechange = handleResponse;
}
</script>
the html:
<form>
<label>Enter a Tag</label><input type="text" name="fm_tag" size="20" /> <br><br>
<input type="Button" name="Enter" value="enter" onclick="postTags()"/>
</form>
and finally the ajaxtest.php page
<?
$tag = $_GET["tag"];
?>
<script type="text/javascript">
alert("<?$tag;">;
</script>
I have a GET request working fine using the same createRequestObject(); script - i can't get my head round why something so simple isn't working when loads of code examples use the same script.
Can anyone help please?
:confused: