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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call function from submit button? 2

Status
Not open for further replies.

rouse01

IS-IT--Management
Joined
Sep 10, 2001
Messages
143
Location
US
Is it possible to call a function from a submit button? I can't get the following to fire:
<html>
<head>
<?php
function func()
{
echo &quot;inside the function&quot;;
}
?>
</head>

<body>
<form name=&quot;test&quot; action=&quot;&quot; method=&quot;POST&quot; onSubmit=&quot;return func();&quot;>
<p>
<input name=&quot;numbr&quot; type=&quot;text&quot; size=&quot;15&quot; maxlength=&quot;12&quot;>
Enter Number</p>
<p>
<input type=&quot;submit&quot;>
</p>
</form>
</body>
</html>

Syntax? I've striped this down to a minimum, but would like to use functions to validate input from a form. Using php version 4.1.2 on apache 1.3.23
Thanks - Keith
 
Your problem is that the php is interpreted before the page gets sent to the browser, the onSubmit bit is done by the browser and the function doesn't exist as far as it's concerned. The only way I can remember off hand of validating form input like this is by Remote Procedure Calls or javascript.
 
KempCGDR is right.
The very nature of PHP is that it is processed before sending the HTML to the browser, hence, Pre-Hypertext-Processor.

However, you can submit the script to itself and check if all required variables are there before deciding what to do next.

All you need is to add at the top of the script:
Code:
if ($HTTP_POST_VARS[submit]){
  # check here and set error condition if necessary
  if (!... whatever you want to check for ...){
      $errorFlag = true;
  }
}

# no errors encountered
if (!$errorFlag){
   # do what the script is dupposed to do 
}

 
Thanks guys. Both get a star for giving me some fundamentals as I slap my forehead. I'm now angling on calling a page-full-of-functions to do a validate on the input fields. Any tips on this technique appreciated. DRJ's example looks promising.

I need to make sure the input form has valid dates. The form accepts the fields as text and I intend to strip non-numeric characters with eregi & rebuild the fields in mm/dd/yyyy format - then test for a valid date. If return invalid, then push the input form back with a msg to re-enter. Otherwise, on to next page.

Keith
 
Consider your audience....

it may be easier and faster to just validate with javascript so you can yell at them right away without redrawing and re-filling out the form.

However, if your audience has the option of disabling javascript, this isn't so great.

If you validate with PHP and push them back to the page on a failure, remember to fill out the portions of the form they already filled out for them! Quite an annoyance when the form comes back up blank.

-Rob
 
Rob,
Good point on the java, I think I'd rather keep validation on the server side. Which brings up a probable future post. I'm calling my functions with an added field_num param so I can track which input field causes the error. I intend to push the page back and highlite (or something) the error-causing fields by examining if the field_num is set when the page is redrawn. Does this make sense, or is there a better technique? Also, would this require session variables, or will my post method ($_post[]) track back?
Keith
 
rouse01,
Have your page write out the input tags like this:
Code:
<input type=&quot;text&quot; name=&quot;myvar&quot; value=&quot;<?echo $myvar;?>&quot;>

To refer to $_POST[myvar] as $myvar register_globals needs to be on. If it's off just have a section that assigns the POST vars to their key:
Code:
foreach($_POST as $key=>$value){
   $$key = $value;
}

POST will include all vars that are posted to the script. Even if you set session vars you would retrieve them (at least once) from the POST.
Setting the value attributes of the fields is what preserves the state for the user.
Be careful with <select>, radio buttons, and checkboxes.
 
Please help me rethink. Good input on one or another field & bad on the other - this my validate page so far...

<?php
function fixdate($fld, $fld_num)
// Input Date, Field Number (for validation)
// expects mmddyyyy or mm/dd/yyyy format
{
$tmp_date = eregi_replace(&quot;[^0-9]&quot;,&quot;&quot;, $fld);

if(strlen($tmp_date) == 8)
{
$tmp_date = substr($tmp_date,0,2).&quot;/&quot;.substr($tmp_date,2,2).&quot;/&quot;.substr($tmp_date,4,4);
$fld=$tmp_date ;
$fld_num='' ;
return $fld ;
}
else
{
$tmp_date=&quot;&quot; ;
echo &quot;<br>I Need a valid date (mm/dd/yyyy) for field &quot;.$fld_num ;
return $fld_num ;
}
}

function datediff($beg, $end)
// Return INTEGER years between dates (mm/dd/yyyy)
{
$begmdy=explode(&quot;/&quot;, $beg);
$endmdy=explode(&quot;/&quot;, $end);
$mm=($endmdy[0] - $begmdy[0])/12 ;
$dd=($endmdy[1] - $begmdy[1])/365 ;
$yy=($endmdy[2] - $begmdy[2]) ;
$diff=intval(($yy+$mm+$dd)*1) ;
return $diff ;
}

$beg=fixdate($_POST[&quot;begdate&quot;],5);
$end=fixdate($_POST[&quot;enddate&quot;],6);

if ($fld_num <1)
{
$age=datediff($beg,$end);
echo &quot;<br>Age: &quot;.$age.&quot; as of &quot;.$end;
}
?>

I hoped to stop the datediff() from firing if fld_num <> 0. I have globals off since that was a recomendation & I know variables are stand-alone inside functions. So is it possible to return the fld_num in my fixdate above?
Creeping along - Keith
 
Suggestion:
In functions you can always refer to a variable that is outside the function by declaring it as global:
Code:
function myfoo($inputvar){
   # refer outside variable
   global $fld_num;
   ....
}
Any manipulation of the globalized variable will be retained outside the function.

track_globals has nothing to do with this making a variable global. It refers to the fact that when set to 'ON' the POST and GET vars are translated into PHP variables named after the array keys e.g. $_POST[foo] can be accessed as $foo. If 'OFF' the vars have to refered to through the $_POST and $_GET arrays.
 
There's a better way t do this foreach($_POST as $key=>$value){
$$key = $value;
}


Just

extract($_POST);



Anikin
Hugo Alexandre Dias
Web-Programmer
anikin@anikin-skywalker.com
 
If you're worried about malicious users be careful using either the
foreach($_POST...)
or the
extract($_POST)
method.

It's relatively easy to slip any values into any variable name this way.

Simple workarounds:
1) Do it by hand...
$junk = $_POST[&quot;junk&quot;];

2) Initialize all the other variables in your script AFTER the foreach or extract command.

-Rob
 
skiflyer,

This is sound advice. Extracting the POST vars like that makes it easier to infuse some arbitrary value and is not any different than having track_globals ON.

It is probaly the safest to have track_globals OFF and to always use $_POST[myvar] in the code. Malicious users could easliy set the value of myvar to anything they want, so it is up to your code to validate if $_POST[myvar] actually contains what you expect it to contain.
 
Thanks all for the excellent advice. Hope I can be of service somewhere down the line.

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top