hi guys, just want to ask if how can i display the value of the textbox in javascript? as in, i will output it

Recommended Answers

All 14 Replies

are you wanting to access the text server side or client side?

A simple clientside access is using javascript. First, the textbox must have a unique id. Then the easiest way to access it is like this:

var boxtext = document.getElementById('uniqueid').value;

thanks, the code really helps,now my problem is that, how could i pass the value of that javascript to php??

php is serverside so it can only be sent using a GET or POST method. This can either be done with forms or with AJAX scripting.

why do you want to send the value to php?

because i need to get the value of the textbox so that i could use it in my sql query..now, that;s my problem, i know its easy if i will just use a button for this..but i have problems with that, thats why, i took this as the solution.

it is probably best to put the text input inside a form and submit it. I'm not sure why you would have any problems using a submit button... but obviously submitting a form will cause a new page to load. Is that your problem - you can't have the page reload or go to another page?

oh, that is really the best way, but this one will fit my program,..could you help me?

I'm trying to help but I'm not sure what your problem is so I don't know what type of coding you need. If you need help making a form, it's pretty simple.

<form action="page.php" method="POST">
<input type="text" name="text1" id="text1" />
<input type="submit" />
</form>

that will submit the contents of the text box back to your server. "page.php" can be any page, even the one that contains the textbox if you code the php properly. To recall the textbox data in php use the $_POST[text1] variable.
Also, be sure to clean the data before adding it to a MySQL string using the mysql_real_unescape_string() function.

Unfortunately, it's now 4am where I live and I'm going to bed... so hopefully that helps. best of luck posting your data back to the server.

oh, thanks to your help sir..but the main problem really is that, passing the value of a variable coming from javascript to php..thats all only..thanks..

oh, thanks to your help sir..but the main problem really is that, passing the value of a variable coming from javascript to php..thats all only..thanks..

<script>
function number(id){
	
		document.location.href = 'xxx.php?id='+id;
	
}

</script>
<a href="javascript:number(10)" class="tahoma11boldlink">number</a>

try this one.

Member Avatar for diafol

I can't see why you don't use a form button. That's what they're for. If you were using an ajax function to get certain info, like synchronising two dropdowns, fair enough, but it seems that you're actually navigating to a new page. For that you may as well use a submit button.

xuexue
Like I said, the only way to pass information to php is with GET or POST and you probably want POST for user input from a textbox. The easiest way to POST is with a form. try this modified version of my previous code.

<form action="page.php" method="POST" onSubmit="return checkForm()">
      <input type="text" name="text1" id="text1" />
      <input type="submit" />
      </form>
function checkForm(){
var output = "";
var elmtext1 = document.getElementById('text1');
output = elmtext1.value + "other stuff";
elmtext1.value=output;
return true;
}

This will cause your javascript to run before the form is submitted and it will submit whatever the "output" is to your php page. On the php receiving page you would have a line like this:
$input = $_POST[text1];
and you should also have this for security reasons to stop SQL injection attacks:
$input = mysql_real_escape_string($input);


If this is not what you need, then you probably want to use AJAX but that is much more complicated. AJAX is designed to change parts of a page's html without reloading the entire page. AJAX uses a combination of javascript, dhtml and php. Although even with AJAX, you still have to use the POST method to submit data back to php, you just do so without using a submit form.

The best way to do this is with AJAX. The basic principle is that you pass data to the server which executes it while continue running the current page, without the need to reload.

This is the code

function createPostAjax(openThis,parameters) 
{
var AJAX = null;			
if (window.XMLHttpRequest)
{
	AJAX=new XMLHttpRequest();		
} 
else					
{ 
AJAX=new ActiveXObject("Microsoft.XMLHTTP"); 
}
if (AJAX==null)						
{ 
alert("No Ajax Support.");	                                       
		return null;					
}
var url= openThis;				
var params=parameters;
	
AJAX.open("POST", url, true);
	
AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
AJAX.setRequestHeader("Content-length", params.length);
AJAX.setRequestHeader("Connection", "close");
	
AJAX.send(params);
	
return AJAX;	
}

function doQuery()
{
var textBox = document.getElementById("ID").value;

url = "query.php";
params = "&action=query&data="+textBox;

var AJAX = createPostAjax(url,params);
AJAX.onreadystatechange	= function()
	{
		if (AJAX.readyState==4 || AJAX.readyState=="complete")
			{ 
				if(AJAX.status == 200)
				{
alert("data Saved");
				}
			}
	}
}

And as for HTML

<textarea id="ID" cols="x" rows="y"></textarea>
<input type="button" id="doSearch" onclick="doQuery()" />

In the php file just $_POST the value and then do the queries.

//K0ns3rv

Member Avatar for diafol

IF you need to use Ajax, perhaps a js 'framework'/thingy like jQuery or Prototype would be useful. It takes care of the response object and simplifies post operations. I hate the idea of having to declare an IE ActiveX as well as the more straightforward XMLHttpRequest.

okei sir..thanks for the help..i will try some alternative to fix this one..it really helped alot ^_^

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.