Forum Moderators: coopster

Message Too Old, No Replies

finding if value is entered in a text box

         

swati

4:43 pm on Nov 30, 2004 (gmt 0)

10+ Year Member



Hello,

I have a text box as follows:
<input type="text" name="<?php echo $usage;?>">

i want to find out if the user has entered anything into this text box.

i tried $_GET['usage']; but it doesnt work

coopster

5:22 pm on Nov 30, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What <form method=""> did you use?

swati

5:37 pm on Nov 30, 2004 (gmt 0)

10+ Year Member



get method

mattx17

5:48 pm on Nov 30, 2004 (gmt 0)

10+ Year Member



> <input type="text" name="<?php echo $usage;?>">

This seems more like what you might be thinking:

<input type="text" name="usage" value="<?php echo $usage;?>">

swati

9:38 am on Dec 1, 2004 (gmt 0)

10+ Year Member



no i have named it like that , its value would be what is entered by the user .........
so i want to find out if a text box named '$usage' has been selected and something written into it

coopster

12:55 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Are you sure the variable is being populated before you first send it out to your form?

View the source after first requesting this form to see what your <input> element looks like. If it shows

<input type="text" name="">
then $usage has not yet been populated and actually you should be getting an error, that's if you have error_reporting() [php.net] turned on in your development mode here.

swati

2:10 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



i checked the 'source' and the name says 'usage0' 'usage1' etc so on according to the for loop .....
so we dont have an error there ......
it is named properly
if the for loop runs 10 times, 10 text boxes with names usage0, usage1 .. will be created .
I want to find out the name of the box in which the user has entered something and what he has entered .....

help!

Regards
Swati

coopster

2:40 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you tried looping through the superglobal(s)?
if (isset($_GET)) { 
print '<pre>';
print_r($_GET);
print '</pre>';
// or perhaps a foreach loop:
print '<pre>';
foreach ($_GET as $name => $value) {
print "$name: $value\n";
}
print '</pre>';
}

swati

3:14 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



well i just tried but it does not give me any information about the usage textbox
why is that?
any other way?

coopster

3:26 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You may have to post your code as this works fine in my tests. There must be something else hindering your process.

swati

3:38 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



<?php
$usage = "usage".$k;
$k++;
?>
<input type="text" size="4" name="<?php echo $usage;?>" maxlength="3"
<?php if($total>100){?>
onBlur="alert('usage exceeds 100%, select another value.');return true"
<?php }?>
onKeyUp="changeVal(this);" onChange="this.form.submit()">

this stuff is inside a while loop : while($row = @mysql_fetch_array($sql))
and just before it $k =0;
and this text box is inside a form with method="get"

coopster

3:57 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Works fine for me, unless I'm missing something...

<html><head><title>Sample</title></head><body>
<h1>Sample</h1>
<form action="<?php print $_SERVER['PHP_SELF'];?>" method="get">
<fieldset>
<legend>Sample</legend>
<?php
$k =0;
// Don't know what total is, so I set it to 50
$total = 50;
// Just used a for loop for demonstration purposes:
for ($i = 0; $i < 10; $i++) {
$usage = "usage".$k;
$k++;
?>
<input type="text" size="4" name="<?php echo $usage;?>" maxlength="3"
<?php
}
?>
<br /><br />
<input type="submit" name="Submit" value="Submit" />
</fieldset>
</form>
<fieldset>
<legend>Posted Form Values<legend/>
<?php
if (isset($_GET)) {
print '<pre>';
print_r($_GET);
print '</pre>';
// or perhaps a foreach loop:
print '<pre>';
foreach ($_GET as $name => $value) {
print "$name: $value\n";
}
print '</pre>';
}
?>
</fieldset>
</body></html>

swati

4:31 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



print_r($_GET) ;
just prints 'Array'
while the foreach loop gives me the value of one get variables .....still not getting anything about usage
my code is very long however still :

while loop starts
{
?>
<tr>
<td class=resourceName>
<?php echo($row['name']);?></td>
<td class=dayNamesStyle>
<table width=75%>
<tr>
<td>
<?php
$sql = @mysql_query("select idTest_Equipment from Test_Equipment where name = '".$row['name']."'");
while($srow = @mysql_fetch_array($sql)){
$idvar = $srow['idTest_Equipment'];
}
$sql1 = @mysql_query("select Project_idProject from Assignment where
Test_Equipment_idTest_Equipment =
'".$idvar."' and monthYear = '".$pass_month[1]."'");

while($prow = @mysql_fetch_array($sql1)){
$idpro = $prow['Project_idProject'];
$sql2 = @mysql_query("select name from Project where idProject = '".$idpro."'");
while($qrow = @mysql_fetch_array($sql2)){
echo($qrow['name']); echo '<br />';
}
}
?><br />
</td>
</tr></table>
</td>
<td class=resourceName>
<form name="myForm" action="<?php echo $_SERVER['PHP_SELF'];?>" method="get">
<input type="hidden" name="site" value="<?php echo $site;?>">
<input type="hidden" name="monthYear" value="<?php echo $HTTP_GET_VARS['monthYear'];?>">
<table>
<tr>
<td class=resourceName>Project:</td>
<td class=resourceName>Usage :(%)</td>
</tr>
<tr><td class=resourceName><?php
$project = "project".$k; #$k++;?>
<select name="<?php echo $project;?>">
<option>select</option>
<option>3G LPM</option>
<option>ZM-FF</option>
<option>Vanquish</option>
</select></td>
<td class=resourceName>
<?php
if($_GET['usage'])
{
$sql4 = @mysql_query("select usage_Per_Month from Assignment where
Test_Equipment_idTest_Equipment = '".$idvar."' and
monthYear = '".$_GET['pass_month'][0]."'");
$total = 0;
while($check = @mysql_fetch_array($sql4)){
$total += $check['usage_Per_Month'];
}
$total += $_GET['usage'];
}
$usage = "usage".$k;
$k++;?>
<input type="text" size="4" name="<?php echo $usage;?>" maxlength="3" <?php if($total>100){?>
onBlur="alert('usage exceeds 100%, select another value.');return true" <?php }?>
onKeyUp="changeVal(this);" onChange="this.form.submit()">
<?php
if($_GET['usage'])
{
$sql_in = @mysql_query("insert into
Assignment(idAssignment, Project_idProject,
date_Of_Assignment,
monthYear,
usage_Per_Month,
Test_Equipment_idTest_Equipment)
VALUES('',
'".$idpro."', curdate(),
'".$pass_month[1]."',
'".$_GET['usage']."', '".$idvar."')") or die(mysql_error());
}
?>
</td></tr></table>
</form>
<?php
if (isset($_GET)){
print_r($_GET);
foreach ($_GET as $name => $value) {
print "$name: $value\n";
}
print '</pre>';
}
?></td>
<?php }?>

please ignore the opening closing of tr, td tags cos i have tried to shorten the code for you n there are far too many nested table sin my page (rest assured there is no error in the markup!)

help!

swati

10:18 am on Dec 2, 2004 (gmt 0)

10+ Year Member



ok lets just forget this long code.....i tried out your code and it worked, but my page has got multiple forms
so when i write print_r($_GET) it does not understand which form's $_GET variable sit has to print
how can i avoid this?

baze22

12:40 pm on Dec 2, 2004 (gmt 0)

10+ Year Member



I think the problem is here:
($_GET['usage'])

there isn't a variable named 'usage', they are all named 'usage'.$num

You'd need to check each variable. Using the sample test code above, I changed the foreach to:

foreach ($_GET as $name => $value) { 
if (strpos($name,'usage')===FALSE){
print "Not the right variable\n";
} else{
if ($value <> ''){
print "$name: $value\n";
} else {
print "$name: empty\n";
}
}
}

baze