Jump to content

[SOLVED] Update textbox value with php


hlstriker

Recommended Posts

I'm trying to update a textbox value with php but it's just giving errors.

 

Here is the code I'm using:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Form test</title>

<script language="JavaScript">
	function UpdateTextBox(percent)
	{
		document.getElementById("test").value = percent;
	}
</script>
</head>

<body>

<form>
<input type="text" id="test" name="test" value="Null" onclick="UpdateTextBox('Testing')" />
</form>

</body>
</html>

<?php
for($i=0;$i<9999;$i++)
{
	// Update textbox with value $i
	UpdateTextBox('$i');
}
?>

 

 

The error is:

Fatal error: Call to undefined function updatetextbox() in C:\xampp\htdocs\formtest.php on line 27

 

Does anyone know how to fix this?

Link to comment
Share on other sites

Well, yes.  You can have a form where you enter in the value, and click a submit button, and use php to populate the textbox with that data, but that's not really "real time." Suppose it depends on what your goals are. If you want something "real time," then you need to stick with javascript, because php is parsed on the server.

Link to comment
Share on other sites

The problem is in your very last lines of code.  You are trying to execute a javascript function within php.

 

<?php
for($i=0;$i<9999;$i++)
{
    // Update textbox with value $i
    ?>
         <script language="javascript">UpdateTextBox('<?php echo $i; ?>')</script>
    <?php
}
?>

No clue why you're trying to do it like this, but this should make it work....all 9999 times LOL

Link to comment
Share on other sites

no...javascript does not get executed on the server.  You cannot put javascript inside a php loop and expect the javascript to be run.  That's not how it works.  Did you read the error he posted?

 

Fatal error: Call to undefined function updatetextbox() in C:\xampp\htdocs\formtest.php on line 27

 

It means javascript got to the line of code where it's calling updatetexbox and it can't find it. 

Link to comment
Share on other sites

CroNiXs code actually works because it's writing a new javascript line as the loop is going. I only wanted to loop 9999 times to see if I could watch the textbox get updated in real time. The result was the textbox updating but so many javascript lines were being executed that the textbox number was lagging behind and sometimes locked the browser up.

 

Thanks for the help everyone.

Link to comment
Share on other sites

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.