Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic use of PHP question 1

Status
Not open for further replies.

Shukusei

Programmer
Sep 6, 2004
43
0
0
CA
I'm working on a little translation script in PHP(my second script written in PHP actually).
I'm quite possitive the script itself works, the problem I'm having though is that I'm not really sure on how to call it..

What I'm wanting to do is take the input in one textfield, translate it, and put the translation into another textfield.

I've done scripts like this in JavaScript, so I figured I could do the same thing and call the translation function from the submit button, and pass input.value to the PHP function, but then I thought "How am I going to send the translation to the other textfield?"

How would I get this to work? I don't want to load a new page, I just want the translation to appear in the other textfield(or some HTML element, that'd work too).

Here's the function I made, if it helps answer my question:
Code:
function translator($origString) {
	$placeHolder = 0;
	$currentChar = "";
	$nextChar = "";
	$transString = "";
	$checkString = "";
	$doublesList = array("ph", "gh", "th", "sh", "ch", "rd", "qu", "ng", "ck");
	
	do {
		$currentChar = $origString{$placeHolder};
		$nextChar = $origString{$placeHolder + 1};
		$checkString = $currentChar + nextChar;
		if (in_array($checkString, $doubleList)) {
			switch ($checkString) {
				case "ph":
					$transString += "ng";
					break;
				case "gh":
					$transString += "ng";
					break;
				case "th":
					$transString += "ch";
					break;
				case "sh":
					$transString += "th";
					break;
				case "ch":
					$transString += "sh";
					break;
				case "rd":
					$transString += "ph";
					break;
				case "qu":
					$transString += "ts";
					break;
				case "ng":
					$transString += "rd";
					break;
				case "ck":
					$transString += "k";
					break;
				$placeHolder += 2;
			}
		else {
			switch ($currentChar) {
				case "a":
					$transString += "i";
					break;
				case "b":
					$transString += "w";
					break;
				case "c":
					$transString += "d";
					break;
				case "d":
					$transString += "p";
					break;
				case "e":
					$transString += "a";
					break;
				case "f":
					$transString += "j";
					break;
				case "g":
					$transString += "l";
					break;
				case "h":
					$transString += "c";
					break;
				case "i":
					$transString += "u";
					break;
				case "j":
					$transString += "f";
					break;
				case "k":
					$transString += "g";
					break;
				case "l":
					$transString += "r";
					break;
				case "m":
					$transString += "t";
					break;
				case "n":
					$transString += "s";
					break;
				case "o":
					$transString += "e";
					break;
				case "p":
					$transString += "b";
					break;
				case "q":
					$transString += "1";
					break;
				case "r":
					$transString += "h";
					break;
				case "s":
					$transString += "m";
					break;
				case "t":
					$transString += "n";
					break;
				case "u":
					$transString += "y";
					break;
				case "v":
					$transString += "z";
					break;
				case "w":
					$transString += "k";
					break;
				case "x":
					$transString += "v";
					break;
				case "y":
					$transString += "o";
					break;
				case "z":
					$transString += "x";
					break;
				$placeHolder += 1;
			}
		}
	} while ($placeHolder <= strlen($origString));
	output.value = $transString;
}
 
PHP is a server-side scripting language that runs on the server before the page is delivered to your web browser. Once the page has been sent to your browser you can no longer use any PHP scripting on that page. The page is now "local" to your computer.

You have to rely on client-side scripting languages (like Javascript, VBScript) to do this kind of task once the server has delievered the page to you.

All the best,
Jeff
 
Thanks for the helpfull respsones. :)
Just out of curiosity though.. How would I go about using a form to send the input from a textfield to the script, have it proccessed, and then sent back in the form of a new page?
ie;

user inputs text to be translated into textfield, hits submit, text gets sent back to the server, server processes it with the function above, then sends back a page with the new translation.

How would that work?
 
The form submits to a page... this page has your script server-side. You "pick up" the value(s) passed into the page (from the form) server-side and assign them to variables (that you can then manipulate as you wish).

Once you have run y our translation script, you can output the contents of a variable to the page (as the value="" part of an input box). The page is then rendered locally with your translated value in place.

First off the page needs some form definition (like this):
Code:
<form method="post" action="pagetwo.php">
<input type="text" name="boxone" />
<input type="submit" value="send" />
</form>

When submitted the form is sent to pagetwo.php you would pick up the passed in value (like this):
Code:
<?
$myPassedValue = $_POST['boxone'];
?>

You would them process the value using your own code... and maybe the result is placed into a new variable ($newVariable) before being echo'd to the page (like this):
Code:
<input type="text" name="boxtwo" value="<? echo $newVariable; ?>" />

If you use method="get" in your initial form, then you would use $myPassedValue = $_GET['boxone'] instead of $_POST. I'm sure there are other ways to do this... but this is the simple way that achieve it.

Hope this points you in the right direction!
Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top