Reputation: 1447
I'm building a basic credit card validator in Codeigniter, here is my HTML:
<input type="text" id="ccard" />
<input type="submit" onClick="check_cc()" name="submit_cc" value="Validar cartão!" id="checkcc" />
<div id="result"></div>
The JavaScript function:
function check_cc() {
var cc;
cc = $("#ccard").val();
$.ajax({
url: '<?php echo base_url() . "js_tests/check_cc_string"; ?>',
type: 'POST',
data: {credit_card : cc},
success: function(output_string) {
setTimeout(function() {
$('#result').append(output_string);
}, 1000);
}
});
}
(at this point I'm not entirely sure if what I'm doing with the URL is actually right. The function seems to trigger on the controller, but not sure if the cc
variable is being passed)
This is my controller:
function check_cc_string() {
$check = check_cc($_POST['cc'], true);
if ($check !== false) {
$output_string = $_POST['cc'] . " - " . $check;
} else {
$output_string = " - Not a match";
}
echo json_encode($output_string);
}
Also inside the controller, the credit card checker:
function check_cc($cc, $extra_check = false) {
$cards = array(
"visa" => "(4\d{12}(?:\d{3})?)",
"amex" => "(3[47]\d{13})",
"jcb" => "(35[2-8][89]\d\d\d{10})",
"maestro" => "((?:5020|5038|6304|6579|6761)\d{12}(?:\d\d)?)",
"solo" => "((?:6334|6767)\d{12}(?:\d\d)?\d?)",
"mastercard" => "(5[1-5]\d{14})",
"switch" => "(?:(?:(?:4903|4905|4911|4936|6333|6759)\d{12})|(?:(?:564182|633110)\d{10})(\d\d)?\d?)",
);
$names = array("Visa", "American Express", "JCB", "Maestro", "Solo", "Mastercard", "Switch");
$matches = array();
$pattern = "#^(?:" . implode("|", $cards) . ")$#";
$result = preg_match($pattern, str_replace(" ", "", $cc), $matches);
if ($extra_check && $result > 0) {
$result = (validatecard($cc)) ? 1 : 0;
}
return ($result > 0) ? $names[sizeof($matches) - 2] : false;
}
Unfortunately, this is returning me this:
Fatal error: Call to undefined function check_cc()
I'm not an expert in these things, but am I right to believe the url passed on jQuery is accessing that PHP function directly? Thus not being able to read the rest of the file?
Is there a practical way to go over this or will I have to rebuild my function to include all of the credit card validation code?
Upvotes: 0
Views: 176
Reputation: 48357
at this point I'm not entirely sure if what I'm doing with the URL is actually right
This only makes sense if the javascript is parsed by PHP before being sent to the client.
Have you really defined functions or are these methods? In the latter case, there is no check_cc() but there might be a $this->check_cc(). It is possible to create a function within a method by declaring it - but the function won't exist until the method is invoked - consider:
class t {
// constructor method....
function t($r) {
print "method called\n";
// inline function defn.....
function t($r) {
print "function called\n";
}
}
}
$a=new t(''); // try commenting this
print t('hello');
Upvotes: 1
Reputation: 36
You are trying to access the $cc as a parameter in check_cc_string function. The $.ajax sends an httprequest to the controller, and the data is passed via $_POST, by default. When calling the check_cc you should use $this->
Try This:
function check_cc_string() {
$c = $_POST['cc'];
$check = $this->check_cc($c, true);
...
Upvotes: 1