ueon Posted June 25, 2011 Share Posted June 25, 2011 I'm trying to pass content, email, user, and content to addpost.php, but it's not returning any values to the 'newupdate' div. <script type='text/javascript'> function addPost(email, user, date) { if(event.keyCode=='13'){ // if they press enter alert(email+user+date); 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.getElementByClassName('newupdate').innerHTML=xmlhttp.responseText; } } var content=encodeURIComponent(document.getElementById('addpost').value); var parameters='content='+content+'&email='+email+'&user='&date='+date; xmlhttp.open('POST', 'addpost.php', true); xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xmlhttp.send(parameters); } } </script> INDEX.PHP echo "<div id='statusbox'><input name='addpost' id='addpost' type='text' style='width: 497px; height: 25px;' onclick='clearBox()' onkeyup=\"addPost('123', '123', '123')\" /></div>"; echo "<div class='newupdate'>"; echo "</div>"; ADDPOST.PHP <?php $email = $_POST['email']; $user = $_POST['user']; $date = $_POST['date']; $content = $_POST['content']; echo "$content"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/240404-passingretrieving-variables/ Share on other sites More sharing options...
sunfighter Posted June 27, 2011 Share Posted June 27, 2011 Your problem is in your parameters string => &user='&date=' see what's missing? try this: var parameters='content='+content+'&email='+email+'&user='+user+'&date='+date; Quote Link to comment https://forums.phpfreaks.com/topic/240404-passingretrieving-variables/#findComment-1235183 Share on other sites More sharing options...
ueon Posted June 30, 2011 Author Share Posted June 30, 2011 I had to change the parameters due to alterations in my processing file, so I can't seem the pass the variable again <script type='text/javascript'> function addfriend(email) { 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.getElementByClassName('newupdate').innerHTML=xmlhttp.responseText; } } var content=encodeURIComponent(document.getElementById('addfriend').value); var parameters='email='+email; xmlhttp.open('POST', 'addfriend.php', true); xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xmlhttp.send(parameters); } </script> echo "<input name='addfriend' id='addfriend' type='button' value='click' onclick=\"addfriend('$email')\" />"; echo "<div class='newupdate'>"; echo "</div>"; Quote Link to comment https://forums.phpfreaks.com/topic/240404-passingretrieving-variables/#findComment-1236938 Share on other sites More sharing options...
sunfighter Posted July 1, 2011 Share Posted July 1, 2011 This line: document.getElementByClassName('newupdate').innerHTML=xmlhttp.responseText; Unless you made this yourself, there is no getElementByClassName. I wouldn't use it here anyway. Change it to getElementById and make the php echo "<div id='newupdate'>"; and it will work. I'd be a little leery about using the same name over and over for different things. => "addfriend" Quote Link to comment https://forums.phpfreaks.com/topic/240404-passingretrieving-variables/#findComment-1237461 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.