SOLVED: Need Help with string modification script.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

SOLVED: Need Help with string modification script.

Post by bironeb »

I use the below code:

Code: Select all

<? $result = $_POST&#1111;func]($_POST&#1111;text1]); ?>
<html>
<head>
<title>Generic Input Results</title>
</head>
<body>
<? echo "$result"; ?>

<p><a href="generic_form.html">Go again!</a></p>
</body>
</html>
And get the following Error:

Fatal error: Call to undefined function: () in c:\apache\htdocs\testing\display_input.php on line 1

I know it has something to do with :

Code: Select all

($_POST&#1111;text1]);
Any ideas?
Last edited by bironeb on Tue Mar 09, 2004 11:21 am, edited 1 time in total.
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

the point is that there is notthign set to $_POST[func] so its trying to call the fuction ($_POST[text1]) and you cant do that...so you need to decare it to be a function so do this:

Code: Select all

<? 
if(isset($_POST[func]))
$result = $_POST[func]($_POST[text1]);
else
$result="no Function declared";
 ?>
<html>
<head>
<title>Generic Input Results</title>
</head>
<body>
<? echo "$result"; ?>

<p><a href="generic_form.html">Go again!</a></p>
</body>
</html>
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

yes it does... php probably thinks that you are trying to call a function with $_POST[func]($_POST[text1])..

what exactly are you trying to do with that line anyway?
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

Well i tried your idea:

Code: Select all

<? $result = $_POST&#1111;'func'].$_POST&#1111;'text1']; ?>
<html>
<head>
<title>Generic Input Results</title>
</head>
<body>
<? echo $result; ?>

<p><a href="generic_form.html">Go again!</a></p>
</body>
</html>
and now all it displays is the a href link Go again!

but no error.

The following is the code from my html page:

Code: Select all

<html>
<head>
<title>Generic Input Form</title>
</head>
<body>
<FORM METHOD = "POST" ACTION = "display_input.php">
<p><strong>Text Field:</strong><br>
<TEXTAREA NAME="text1" COLS=45 ROWS=5 WRAP=virtual></TEXTAREA></p>
<p><strong>String Function:</strong><br>
<input type="radio" name="func" value="md5" checked> get md5<br>
<input type="radio" name="func" value="strlen"> get length of string<br>
<input type="radio" name="func" value="strrev"> reverse the string<br>
<input type="radio" name="func" value="strtoupper"> make string uppercase<br>
<input type="radio" name="func" value="strtolower"> make string lowercase<br>
<input type="radio" name="func" value="ucwords"> make first letter of all words uppercase</p>

<p><input type="submit" name="submit" value="Do Something With the String"></p>
</form>
</body>
</html>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Code: Select all

<?php
$result = '';
if(!empty($_POST['func'])){
  $result = $_POST['func']($_POST['text1']);
}
?>
<html>
<head>
<title>Generic Input Results</title>
</head>
<body>
<?php echo $result; ?>
<p><a href="generic_form.html">Go again!</a></p>
</body>
</html>
Be very careful with this code if it's on a public server, it wouldn't be difficult to post functions that you're not allowing for, exec, eval etc..
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

ok so now i tried:

Code: Select all

<?
if(isset($_POST&#1111;func]))
$result = $_POST&#1111;func]($_POST&#1111;text1]);
else
$result="no Function declared";
?>
<html>
<head>
<title>Generic Input Results</title>
</head>
<body>
<? echo $result; ?>

<p><a href="generic_form.html">Go again!</a></p>
</body>
</html>
And after i submit from the html page i get:

no Function declared

Go again![/url]
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

Ok tried the 3rd suggestion:

Code: Select all

<?php 
$result = ''; 
if(!empty($_POST&#1111;'func']))&#123; 
  $result = $_POST&#1111;'func']($_POST&#1111;'text1']); 
&#125; 
?> 
<html> 
<head> 
<title>Generic Input Results</title> 
</head> 
<body> 
<?php echo $result; ?> 
<p><a href="generic_form.html">Go again!</a></p> 
</body> 
</html>
and it only displayes:

Go again!

hmmm, any other ideas?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

It works fine for me. Are you pressing 'enter' to submit the form or pressing the button? Also double check your html, the below html form worked ok with the above php code.

Code: Select all

<html>
<head>
<title>Generic Input Form</title>
</head>
<body>
<FORM METHOD = "POST" ACTION = "display_input.php">
<p><strong>Text Field:</strong><br>
<TEXTAREA NAME="text1" COLS=45 ROWS=5 WRAP=virtual></TEXTAREA></p>
<p><strong>String Function:</strong><br>
<input type="radio" name="func" value="md5" checked> get md5<br>
<input type="radio" name="func" value="strlen"> get length of string<br>
<input type="radio" name="func" value="strrev"> reverse the string<br>
<input type="radio" name="func" value="strtoupper"> make string uppercase<br>
<input type="radio" name="func" value="strtolower"> make string lowercase<br>
<input type="radio" name="func" value="ucwords"> make first letter of all words uppercase</p>

<p><input type="submit" name="submit" value="Do Something With the String"></p>
</form>
</body>
</html>
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

I tried both ways, Pressing ENTER after i choose radial, and clicking the button... but it only displays the URL the $result isn't printing, im confused.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Is this online somewhere we can see it running?
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

No, sorry its running on the apache server on my computer only... I hit it by going to:
http://127.0.0.1/testing/generic_form.html
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

What version of PHP? If it's => 4.1.0 then repost the exact code you have now, if it's less than 4.1.0 then don't bother, it won't work ;)
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

well i go to:
http://127.0.0.1/phpinfo.php

and it says:

PHP Version 4.0.5

Is this my problem? if so why? and how would I upgrade?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Yeah, that's the problem. $_POST didn't come into existance until 4.1.0.
Just goto http://php.net and download the latest stable version, installation/upgrade instructions will be in the file you download. What OS are you on by the way (windows, *nix, etc..) ?
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

Thanks markl999 I'll upgrade and try again. I'm using Windows XP btw.
Post Reply