UnknownPlayer Posted February 23, 2011 Share Posted February 23, 2011 I have this form: <textarea id="pp" name="post_text" rows="1" cols="50"></textarea><br /> <input type="submit" value="Post" onClick="postit()" /> <div id="posted"></div> this is postit() js code: <script type="text/javascript"> function postit(){ var post_text = document.getElementById('pp'); if (post_text != "") { 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("posted").innerHTML=xmlhttp.responseText; } } var data = "content="+post_text; xhr.open("POST", "send_post.php", true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send(data); } } </script> .. and this is send_post.php code: <?php include("includes/connection.php"); ?> <?php $content = $_POST['content']; $content_date = time(); $query = "INSERT INTO wallposts (post, date) VALUES ('{$content}', '$content_date')"; $result = mysql_query($query); echo "Posted"; ?> Now, js code gets in post_text var, text of "pp" input value, but it wont send it to send_post.php(POST method) and write at mysql :S Can someone help me? Thanks.. Link to comment https://forums.phpfreaks.com/topic/228543-php-ajax-js-help/ Share on other sites More sharing options...
jcbones Posted February 23, 2011 Share Posted February 23, 2011 Are there JS errors? Have you de-bugged it, (I usually use alert's to debug JS)? I bet you will find out that you are not getting the text from the text area. var post_text = document.getElementById('pp').value; Link to comment https://forums.phpfreaks.com/topic/228543-php-ajax-js-help/#findComment-1178417 Share on other sites More sharing options...
UnknownPlayer Posted February 23, 2011 Author Share Posted February 23, 2011 I dont get any error :S post_text is getting this value: [object HTMLTextAreaElement] :/ but this code works: var post_text = document.getElementById('pp').value; But POST method doesn't work :S Can someone help me ? Link to comment https://forums.phpfreaks.com/topic/228543-php-ajax-js-help/#findComment-1178598 Share on other sites More sharing options...
jcbones Posted February 24, 2011 Share Posted February 24, 2011 Have you tried sending the post data to a full url, and not just a relative file? example: //instead of xhr.open("POST", "send_post.php", true); //try xhr.open("POST", "http://mysite.com/full/path/to/send_post.php", true); Link to comment https://forums.phpfreaks.com/topic/228543-php-ajax-js-help/#findComment-1179298 Share on other sites More sharing options...
UnknownPlayer Posted February 26, 2011 Author Share Posted February 26, 2011 Still same :S Can u give me example of sending content with post method ? Link to comment https://forums.phpfreaks.com/topic/228543-php-ajax-js-help/#findComment-1180026 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.