Forum Moderators: coopster

Message Too Old, No Replies

Questions about stopping and beginning the next function

         

tech0925

4:55 am on Mar 1, 2011 (gmt 0)

10+ Year Member



I cannot figure out for nothing how you separate different commands or functions. Lets say I want to check to make sure my form had a topic entered and then once it passes it continues on to check the email field for injection. How do I do that? I did not copy my whole script here, just the end of one to the next.

Thanks!

if(!filter_has_var(INPUT_POST, "topic"))
{
echo("You did not enter a topic.");
}
else



function spamcheck($field)



Also, lets say i had a code to remove all html tags and inserted that after the spamcheck was complete. Will the form know to send the email with all these corrections or do I need to output something after each correction is made? I am completely confused and trying to learn this.

eelixduppy

11:43 pm on Mar 1, 2011 (gmt 0)



I am somewhat lost as to what you are asking. Excuse me if I am misunderstanding your question.

A web-form usually undergoes two types of checks before the data that it contains gets used in some useful way. The first type of checking is client-side (e.g., with JavaScript) and is beneficial because it reduces server load while also letting the user know immediately if something is not correct in a form field. The second type of checking is server-side (e.g., with PHP), and this is ensure that the data matches some criteria before it can be used; it is important to have at least this server-side validation because there are methods to circumvent the client-side checks.

Assuming you are talking about the server-side validation, this is done all at once after the form has been submitted (in most instances). You code would then look something like this (simplified, of course):


if(isset($_POST['submitted'])) {
$error = 0;
$error += check_topic($_POST['topic']);
$error += check_email($_POST['email']);
// etc...

if($error > 0) {
print_error_msg();
} else {
// use data in useful way
}
}


Obviously the checks can be made in many different ways, especially when it comes to error messages, etc... but this is a simple example. Hope this helps.

rocknbil

5:18 pm on Mar 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, most PHP coders work linear, from top to bottom of file. Look at this modified version of E.L.'s code:


You will always have output from a program. So you can store the output of various functions and echo it once.


// Configurables at the top, where we can find them.
$title='Post a Topic';
$errtitle='Error Posting Topic';
$successtitle=Topic Successfully Posted';
define ('MYSCRIPT', 'my-script.php');
// So we can access it without globals in functions.
// This is, after all, a constant. :-)
//
if(isset($_POST['submitted'])) {
$error = check_input();
if($error) { $output = output_form($errtitle,$error); }
else {
add_topic();
$output = output_success($successtitle);
}
}
else { $output = output_form($title); }
header("content-type:text/html");
echo $output;
exit; // retentive and optional :-)


That's it, that's your entire program. Below those you would have these functions, the first of which will return null if there are no errors.


function check_input() {
$err = null;
// here you go through all variables in global $_POST
// throw away what you don't need
// cleanse them, removing malicious characters
// if some error is found, append it to $err, for example,
// if ($_POST['some_required_field']=='') { $err .= "<li>The some_required_field is required.</li>"; }
return $err;
}


Note $err is null if there's no errors, which allows us to do . . .

if($error) { . . . .

Then we pass both the $title and $error to output_form. Being optional, we set the error's default value in the input parameter to null, and using the same concept, if there is an error value, output it above the form. Note I'm using list items allowing you to trap ALL errors at once.


function output_form($formtitle,$errors=null) {
$form = '<form action="' . MYSCRIPT . '" method="post">';
if ($errors) {
$form .= '<p class="warning">There are errors with your input:</p><ul> . $errors . '</ul>';
}

// you know the drill, build your form
$form .= '<p><input type="submit" value="submit"></p></form>';
// A useful bit here - another function you would write
// to put $form and $formtitle into a template.
$form = insert_to_template($formtitle,$form);
return $form;
}


Below that you would have a function for ooutput_success(), as well as the insert_to_template() function . . . etc. This isolates each area of programming to make it so much easier to debug as your programs grow.

tech0925

5:28 pm on Mar 2, 2011 (gmt 0)

10+ Year Member



Thank You, this helps alot everyone..