doufeixi6014 2012-11-13 12:25
浏览 45
已采纳

jQuery Ajax和PHP - 可以在PHP中调用其他函数吗?

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?

  • 写回答

2条回答 默认 最新

  • drutjkpsr67393592 2012-11-13 12:44
    关注

    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);
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)