Add content

Am using the following bit of code to allow users to add new content (html input fields), and it’s working well.

But, I would like for it to work multiple times, not just once.

Currently, it allows a user to click on Add Item, adds the content, but then won’t work again.

Any ideas? (Am more a PHP person than AJAX/Javascript, but think the latter is probably the route to go).

<script>
	function loadDoc(url, cFunction) {
		var xhttp;
		xhttp=new XMLHttpRequest();
		xhttp.onreadystatechange = function() {
			if (this.readyState == 4 && this.status == 200) {
				cFunction(this);
			}
		};
  	xhttp.open("GET", url, true);
  	xhttp.send();
	}
	function myFunction(xhttp) {
		document.getElementById("demo").innerHTML =
		xhttp.responseText;
	}
</script>

<p id="demo">
	<label>Add New Content</label>
		<button type="button" onclick="loadDoc('content.php', myFunction)">Add Item</button>
</p>

The code is doing what it was written to do, replacing/setting the id=“demo” element with whatever is returned by the php code. So, the question becomes, what result do you want?

I’d like for it to work more than once. ie press or select ‘Add Item’, and it adds content again, and so on, each time the button is pressed.

Sponsor our Newsletter | Privacy Policy | Terms of Service