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

<html>
<head>
<title></title>
<script src="jquery-1.4.2.js" type="text/javascript">
</script>
<script type="text/javascript">
$(document).ready(function() {
	$("div#msgbox").hide();
	$("#addComment").click(function (){
		$.post('index1.php', {
			name:$("#name").val(), //PHP recognize it like POST
			comment:$("#comment").val() //PHP recognize it like POST
		},
		function(response){
			$("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
	}
	)
});
</script>
<style type="text/css">
body{
margin:0px;
}
#msgbox{
height:50px;
width:100%;
background:#999;
position:fixed;
}
</style>
</head>
<body>
<div id="msgbox">
<span id="msg">
</span>
</div>
<form id="commentForm">
Name: <input type="text" id="name" />
Comment: <input type="text" id="comment" />
<input type="submit" id="addComment" value="Post Comment" />
</form>

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.

Member Avatar for rajarajan2017

Javascript will not post the value to php

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

Member Avatar for rajarajan2017

You comment on code shows that php will recognize that

Member Avatar for rajarajan2017

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" />
</form>

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

$value=$_POST['txtvalue'];

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
req.send(null);
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
     }
   }
   clear();
}

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
http://www.ibm.com/developerworks/web/library/wa-ajaxintro1.html

Member Avatar for rajarajan2017

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.

$id=$_GET['id'];
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.