I am new to Javascript and I don't want to use frameworks. I found a tutorial on how to make this:

<script src="jquery-1.4.2.js" type="text/javascript">
<script type="text/javascript">
$(document).ready(function() {
	$("#addComment").click(function (){
		$.post('index1.php', {
			name:$("#name").val(), //PHP recognize it like POST
			comment:$("#comment").val() //PHP recognize it like POST
			$("div#msgbox #msg").html("<h2>Comment added!</h2>");
			$("div#msgbox").hide().slideDown("normal"); //slideDown or shoe
		return false;
	$("div#msgbox").click(function() {
		$(this).slideUp("normal"); //slideUp or hide
<style type="text/css">
<div id="msgbox">
<span id="msg">
<form id="commentForm">
Name: <input type="text" id="name" />
Comment: <input type="text" id="comment" />
<input type="submit" id="addComment" value="Post Comment" />

It takes name and comment value and sends it as $_POST and $_POST to post.php. I was wondering can somebody write this in raw javascript.

Recommended Answers

All 8 Replies

Member Avatar

Javascript will not post the value to php

But jQuery is coded in Javascript. How is it then possible in jQuery?

Member Avatar

You comment on code shows that php will recognize that

Member Avatar

In java Script you can use like this: (HTML)

<form name="frm1" method="post" action="post.php">
Name: <input type="text" id="name" name="txtvalue"/>
<input type="submit" id="addComment" value="Post Comment" />

The submit button will post the element values to the php page thats it. you can receive value by specifiying the below code in post.php


you got it. Simple.

Is there a way to this but not to post values to post.php but to send it with get method?

You want to make an ajax call like this:

var req;
var url = "location.php?name="+valueHere;
if(window.XMLHttpRequest)//this is to determin if IE or other
   req = new XMLHttpRequest(); 
}else if(window.ActiveXObject) //this is the IE branch
 req = new ActiveXObject("Microsoft.XMLHTTP");
req.open("GET",url,true); //could have been POST
req.onreadystatechange = callback; //this is the state change event of the http request
function callback(){
  if(req.readyState == 4){
     if(req.status == 200){
        //do something with req.responseText which is the stuff 
        // this is the data that your php sends back

Now this isn't exactly the answer to your question but it is how ajax is done without the framework in javascript. You may want to check out some basic ajax tutorials such as these: http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

Member Avatar

Is there a way to this but not to post values to post.php but to send it with get method?

Yes you can, instead of method=POST use GET. and receive thru your url.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.