Jump to content

Recommended Posts

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";
?>

 

Link to comment
https://forums.phpfreaks.com/topic/240404-passingretrieving-variables/
Share on other sites

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>";

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"

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.