Jump to content


Photo

Counting Characters in variable returned.


  • Please log in to reply
4 replies to this topic

#1 jimbob33

jimbob33
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 28 October 2006 - 04:53 PM

Hi there,

I'm trying to write a php echo file to go with a html form.
I need to count and show the total number of characters returned from the variables entered on the form.

Where do I insert this and do I use count() or strlen() or neither?  This may be simple but I'm new to php and struggling.

Here is my php code so far - not sure if you need the code from my html form aswell.

Thanks in advance.

<html>
<body>

Your favourite book is <?php echo $_GET["book"]; ?>.<br />
Your favourite meal is <?php echo $_GET["meal"]; ?>.


</body>
</html>

#2 Skatecrazy1

Skatecrazy1
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts
  • LocationSouthern California

Posted 28 October 2006 - 05:00 PM

Well how exactly are you retrieving the data from the user?  because GET will only work if you use
method="url" in the form.  I suggest using the post method, then the way to count the string length of the submitted stuff is:
<?php
$fav_book = $_POST['book'];
$fav_meal = $_POST['meal'];
$book_length = strlen($fav_book);
$meal_length = strlen($fav_meal);
//then echo it
echo("<p>Your favorite book is $fav_book.</p>");
echo("<p>Your favorite meal is $fav_meal.</p>");
//and use the length variables however you want, you didn't really go into detail on that
?>

plus, the count function is used to count the number of values in an array, so it wouldn't be of use here.

fuck it or fight it

#3 jimbob33

jimbob33
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 28 October 2006 - 05:13 PM

Thanks for this but I have to use the 'get' method for college work. It's not for a live system.
I am using it on a local web server - my own PC  - so files are in the same place.






#4 Skatecrazy1

Skatecrazy1
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts
  • LocationSouthern California

Posted 28 October 2006 - 05:16 PM

well if someone is requiring you to use the get method, then just do the same thing as above, but change your form method to url and replace $_POST with $_GET and it should work
fuck it or fight it

#5 jimbob33

jimbob33
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 28 October 2006 - 05:43 PM

Works great - thanx for your help.  :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users