I am now implementing a Ajax and the server side programming language is PHP. I need to create a Ajax form verifying and input system for my web app.
$redirectPage = $redirectTime = '';
if($a == 'register') {
$data = array('name', 'username', 'password', 'gender', 'emailaddress');
$required = array('name', 'username', 'password');
foreach($data as $field) {
if(in_array($field, $required) && (!isset($_POST[$field]) || $_POST[$field] == '')) {
$type = 'danger';
$message = 'Please enter all required fields.';
// *1
} else {
$$field = $_POST[$field];
}
}
// Continue with register process
// Set the redirectPage and redirectTime for JavaScript setTimeout here
} else {
$type = 'warning';
$message = 'You usually won\'t be able to see this. But if you did, that means you are trying to hack my website!';
return;
}
$retval = json_encode(
array(
'Type' => $type,
'Message' => alert($type, $message), // my own function, not important here
'RedirectPage' => $redirectPage,
'RedirectTime' => $redirectTime,
)
);
echo $retval; // The value here will send back to my Ajax
As you see I didn't stopped the handle process even though the post field is not in the required array. Finally it will continue with the rest process.
For *1: This is the place I have tried to put some reserved words here.
continue: For an experienced programmer, you should be noticed that it will only get out of this if condition and continue the next foreach loop.
exit: The rest of the code won't process, by all means nothing to return to my Ajax until its timed out.
break: I thought it will get out of
if($a == 'register')
but it didn't happened.
goto: I don't want to be eaten by dinosaurs except its the only way to do that.
For the rest of the code add an if condition check whether
has been set to 'danger': I don't wanna do this way ... (But if this is the only way ...)$type
Thank you.
I'm not entirely sure I understand the question, but I believe the issue you're having can be solved by adding some scope. I refactored your code in hopes that it will point you to the right direction.
function sendResponse ($type, $message, $redirectPage, $redirectTime) {
$retval = json_encode(
array(
'Type' => $type,
'Message' => alert($type, $message), // my own function, not important here
'RedirectPage' => $redirectPage,
'RedirectTime' => $redirectTime,
)
);
echo $retval; // The value here will send back to my Ajax
}
function sendRequiredResponse ($redirectPage, $redirectTime) {
$type = 'danger';
$message = 'Please enter all required fields.';
sendResponse($type, $message, $redirectPage, $redirectTime);
}
function sendHackResponse ($redirectPage, $redirectTime) {
$type = 'warning';
$message = 'You usually won\'t be able to see this. But if you did, that means you are trying to hack my website!';
sendResponse($type, $message, $redirectPage, $redirectTime);
}
function sendRegisteredResponse ($redirectPage, $redirectTime) {
$type = 'success';
$message = 'Registration successfull';
sendResponse($type, $message, $redirectPage, $redirectTime);
}
function register($redirectPage, $redirectTime) {
$data = array('name', 'username', 'password', 'gender', 'emailaddress');
$required = array('name', 'username', 'password');
foreach($data as $field) {
if(in_array($field, $required) && (!isset($_POST[$field]) || $_POST[$field] == '')) {
/**** registration stops *****/
return sendRequiredResponse($redirectPage, $redirectTime);
}
$$field = $_POST[$field];
}
// Continue with register process
// Set the redirectPage and redirectTime for JavaScript setTimeout here
sendRegisteredResponse($redirectPage, $redirectTime);
}
$redirectPage = $redirectTime = '';
if($a === 'register') {
register($redirectPage, $redirectTime);
} else {
sendHackResponse($redirectPage, $redirectTime);
}