Jump to content

[SOLVED] Checking the length of a POST variable


soycharliente

Recommended Posts

[code]<textarea id="data" name="data" rows="4" cols="35"></textarea>[/code]

I'm trying to check to see whether or not the user entered any data into a textarea field and don't know how to do so. I tried using isset(), strlen(), count(), and other things from the php.net site, but nothing seems to work. Will these things logically not work? Meaning, is it even possible for PHP to interact with this type of HTML? I Google'd "how to measure the length of input from textarea with PHP" and nothing useful came back.

And points in the correct direction or a better tutorial/informative page?
Link to comment
Share on other sites

php is a server side language and html / javascript are client side languages

in other words all the php is parsed and run before it even gets to your client side scripting language, therefore if you want php to interact with your html and javascript code you need to send it away and bring it back again so the php can check it and then forward the answers to the client side. or check it with a javascript function.
Link to comment
Share on other sites

I'm pretty sure that's what I'm doing. Maybe I should have provided more code.

[code]<?php
session_start();

$loggedIn = FALSE;
$checkform = FALSE;
$formsent = FALSE;
if (isset($_SESSION['LoggedInUser'])) {
$loggedIn = TRUE;
}
if ($_GET['action'] == "logout") {
session_destroy();
$loggedIn = FALSE;
} else if ($_GET['action'] == "checkform") {
$checkform = TRUE;
}

if ($checkform){
$data= $_POST['data'];
if (strlen($data) < 1) {
echo "<p>" . strlen($data) . "</p>";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>charlieholder[dot]com</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" title="Normal" type="text/css" media="screen" href="./css/screen.css" />
<script type="text/javascript" src="./js/main.js"></script>
</head>

<body>
<?php if (!$formsent) { ?>
<form action="contact.php?action=checkform" method="post">
<textarea id="data" name="data" rows="4" cols="35"></textarea>
<input class="button" type="submit" name="Submit" value="Send" /> &nbsp;
<input class="button" type="reset" name="Reset" value="Clear" />
</form>
<?php } else { ?>
<p>Thank you for your comments. I will get back to you as soon as I can.</p>
<?php } ?>

</body>
</html>[/code]

This is what I have that's not working.
Link to comment
Share on other sites

i take it that you are trying to echo out the length of the dat string by using this function
[code]
<?php
if ($checkform){
$data= $_POST['data'];
if (strlen($data) < 1) {
echo "<p>" . strlen($data) . "</p>";
}
}
>?
[/code]

the rpoblem being is that you ave this line at the top

[code]
<?php
$checkform = FALSE;
?>
[/code]

so, you have set $checkform to equal false then in your function you are testing to see if its true before running your code(line 1 of your copied code above)
Link to comment
Share on other sites

But upon submitting the form, the user is taken to contact.php?action=checkform, found in the action attribute of the form. Underneath
[code]$checkform = FALSE;[/code]
I do
[code]if ($_GET['action'] == "checkform") {
$checkform = TRUE;
}[/code]
before it gets to the
[code]if ($checkform)[/code]
so it will be true if the click to submit the form.

So to me it seems like something else is the problem.
Link to comment
Share on other sites

there is no such thing as a $_POST variable. $_POST is just an array that happens to be both global and populated with $_POST data that is parsed from the http_request headers.

I know you and some others will might say "i knew that" But I think ppl exponentially beginners don't realize how simple it really is. It is just an array, so it acts like an array. I have come across tutorials in the past that teach them as variables and I think it is misleading to beginners. Why it is obvious now I have made a point that its an array it might be something beginners otherwise would not think about as they would just take for granted its a variable.

$_POST
$_GET
$_SERVER
$_REQUEST
$_SESSION
$_COOKIE
$GLOBAL

All of these are just arrays an to those that might used the excuse well there are GLOBAL variables, there is also a global keyword.. So its a little different but the fact remains it is really nothing more than a global ARRAY
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.