duan051347 2015-08-01 18:13
浏览 35

jQuery使用PHP,但收到的“数据”不会在正确的块中执行

Thanks to the comments, I added the PHP script too - and edited the echo text, thanks to another comment :)

This is the final statement here, as I've achieved what I wanted. The explanation is in the comment section, and I've added the new code below. Thanks!

I'm having serious issues, because my code was working before I decided to change a "project's design layout" with jQuery, and some Bootstrap. My jQuery ($.post()) to PHP works fine, this is considered because I receive a response - data is inserted, validation failed, or server error. The main issue here is the response that jQ receives. The snippet of code is below, but let me stress what is going on here.

It's 3 options (responses) I'm expecting from PHP which do return as datatype string - heed quoted text:

A) "revise_input" - not a valid email, or only a-zA-Z for names etc. B) "failed_insert" - server technical errors SQL || PHP. C) This response is a string related to the subject selected by the user upon success. For clarity the user posts a message with the subject feedback, and in return PHP will echo "Thank you for sending feedback......" opposed to "Thank you for reporting a fault" etc. These are successful - after insert->id is established - text done with a switch. A and B are both successful executions with PHP, but they need to be appropriately executed by jQuery.

jQuery will not execute the right code block thereafter - it was working before changes, and I stupidly forgot to save a backup file.

    $(document).ready(function(){ // I've refrained from adding additional code above, but this where the trouble starts
    $.post('php/validate_input2.php', { nm : nm, lnm : lnm, em : em, um : um, jh : jh, us : us, cb : cb}, function(data, status)
    {
        if(status === "success")// it will enter this code block without fault
        {
            if(data == "failed_insert" || data == "revise_input")// heed the comparison operators. I've tried === too but to no avail. jQuery will ignore this block of code even when I know the data echo'd is either 1.
            {
                if(data == "failed_insert")// as above with === I've tried it
                {
                    alert("Server technical issues: " + data + " " + status); // I've left out all the fancy jQ magic for now if "failed_insert", but for testing I want to see this response here "as is".

                }
                else
                {
                    alert("Fields have failed validation: " + data + " " + status); // I don't see this code block getting executed neither

                }
            }
            else
            {
                alert("Data has been inserted: " + data + " " + status); // this is where EVERYTHING gets executed. This should only be shown if its not "revise_input" || "failed_insert" but one of various messages upon success of insert.
            }
        }
        else
        { 
            alert("Failed or other data response; irrelevant for now");//as the alert suggests, no issue here.
        }
    });

As you can see for yourselves, there shouldn't be an issue here. I even tested the types that are sent back and forth between PHP and jQ = string. This is what I get, but replace the ? with "revise_input" || "failed_insert" || "thank you for sending something blah blah blah...." - windows alert box:

The page at localhost says: "Data has been inserted: ? success"

Here is the PHP script:

      if($is_set === false){
         echo "revise_input"; // this code block is executed if email isn't valid - of type string right?
      }else{
      switch($us){ // what to send back, after the script differentiates the users subject
      case 'general':
        $is_stored = "Great, your message was submitted successfully. You may receive an email response if a valid email was used.";
     break;
     case 'feedback':
        $is_stored = "Thank you for your feedback. We appreciate all submissions, and may respond if necessary.";
     break;
     case 'ideas':
        $is_stored = "Thank you for submitting your idea. Have you read about our terms on submitting <a href='privacy.php?from=contact_centre.php&subject=ideas&message=success#disclosure' alt='Privacy: Submittions'>ideas?</a>";
     break;
     case 'fault':
        $is_stored = "Thank you for reporting a fault. We will endeavour to respond to this issue as soon as possible, if necessary.";
     break;
     default:
        $is_stored = "Great, thank you for contacting us. You may receive an email response if a valid email was used.";
    }

    // everything below works fine, as I can see in my database using PHPMYADMIN during development

    $vi_stmt = $con->prepare('INSERT INTO contact_centre (contact_name, contact_lastname, contact_email, contact_subject, contact_body, contact_news, contact_ip, contact_uua, contact_time, response_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
    $vi_stmt->bind_param('sssssissis', $nm, $lnm, $em, $us, $um, $cb, $ip, $ua, $dt, $st);

    if($vi_stmt->execute() && $vi_stmt->insert_id){ 

    if($cb == 1){
        $vi_stmt = $con->prepare('SELECT e_id FROM e_news WHERE e_mail = ? LIMIT 1');
        $vi_stmt->bind_param('s', $em);
        $vi_stmt->execute();
        $vi_stmt->store_result();

        if($vi_stmt->num_rows < 1){
            $vi_stmt = $con->prepare('INSERT INTO e_news (e_known, e_mail, e_name, e_lastname, e_confirm, e_token, e_string, e_ip, e_time, e_ua) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
            $vi_stmt->bind_param('isssssssis', $cb, $em, $nm, $lnm, $st, $et, $es, $ip, $dt, $ua);
            $vi_stmt->execute();
        }else{
            $vi_stmt->bind_result($id);
            $vi_stmt->fetch();
            $vi_stmt = $con->prepare('UPDATE e_news SET e_known = ?, e_name = ?, e_lastname = ?, e_confirm = ? WHERE e_id = ?');
            $vi_stmt->bind_param('isssi', $cb, $nm, $lnm, $vst, $id);
            $vi_stmt->execute();
        }
    }

    if($us !== 'fault'){
        $start_end = time() . '_' . strtotime('+6 hours', time());
        setcookie('secure', $start_end, time() + 10800, '/'); // expires in +6 hours from execution; denies spamming, robots etc - fault reports allowed
    }

    echo $is_stored; // if insert is successful, it will echo the specific thank you

    }else{
    // else it will echo failed_insert - as in no insert, or execution etc
      echo "failed_insert";
    }
    } ?>

What's gone wrong, anyone? Thank you in advance.

// everything below has been successful, it's just a snippet of what was changed. This is the js file:

    $.post('php/validate_input.php', { nm : nm, lnm : lnm, em : em, um : um, jh : jh, us : us, cb : cb}, function(data){
      if(data !== '3' && data !== '2'){
        $('#hbContact').css('color', 'green').html(data);
      }else{
        if(data === '2'){
                    $('#hbContact').css('color', '#ff0000').html('Oops something went wrong. If the problem persists, please do try again later.');
        }else{
          $('#hbContact').css('color', '#ff0000').html('Oops something went wrong. Double check your email address; heed it\'s alphabet characters only for name.');                    
            }
        }
    });

You can imagine my frustration, because this seems a little too simple to create; I'm sure it had something to do with the comparison operators, and datatypes. The PHP echo's: 3 || 2 || "the text string". The "text" is of type string, and the numbers pure integer. I let JS claim the numbers "string" to make life easier after using typeof data.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源