dsdsm2016 2013-09-30 19:02
浏览 59
已采纳

PHP函数忽略if语句

Due to one agent wanting his website url on the functionality that I worked on a month ago I ended up having to make some minor changes. I have two function PHP pages that run a very similar script but I had to make two based of two value sets. What they echo onto the page with AJAX is exactly the same and this is where it gets a little weird...

The first script I did was successful but I needed to make a if elseif else statement so everyone agent didn't have a link that went no where. After fiddling around with this statement I was able to get just the one agent to have his website URL on there. Once I had that done I was under the impression that it would be smoothing sailing from there..it was not...

I used the exact same statement for both of their scripts and only one works. The only thing that differs from them is what value it is receiving and that I use JavaScript + AJAX for the first one (Which works) and then decided to learn jQuery + AJAX to do the next one. Before this they all worked and it is the exact code for both besides the use of JavaScript/jQuery (which is the same language) and one uses GET while the other uses POST

I also get no errors or anything while the function is running. The agent's name is Sam Fiorentino that is the only one with a website url. I went into the console for the second search, the radio buttons, and it shows the company name outside of the anchor tag which is the root of the problem. Why would one display it correctly while the other doesn't?

First PHP (Works)

while ($stmt->fetch()) { // Gets results from the database
                echo "<div class='agentcon'>" . "<span class='agentn'>" . "<strong>". $First_Name . "&nbsp;" . $Last_Name . "&nbsp;" . $Suffix . "</strong>" . "</span>" . "<a href=mailto:".$Email . ">" . "<span class='email'>" . "Send an e-mail to" . "&nbsp;" . $First_Name . "</span>" . "</a>" ."<div class='floathr'></div>";
                if ($Company == NULL) {
                    echo "<p>";
                }
                elseif ($Website == NULL) {
                    echo "<p>" . "<strong>" .$Company . "</strong>" . "<br>";
                }
                else {
                    echo "<p>" . "<strong>" . "<a target='blank' href=" .$Website . ">" .$Company . "</a>" . "</strong>" . "<br>";
                }

Second PHP (Doesn't Work)

while ($stmt->fetch()) { // Gets results from the database
                echo "<div class='agentcon'>" . "<span class='agentn'>" . "<strong>".$First_Name . "&nbsp;" .$Last_Name . "&nbsp;" . $Suffix . "</strong>" . "</span>" . "<a href=mailto:".$Email . ">" . "<span class='email'>" . "Send an e-mail to" . "&nbsp;" .$First_Name . "</span>" . "</a>" ."<div class='floathr'></div>";
                if ($Company == NULL) {
                    echo "<p>";
                }
                elseif ($Website == NULL) {
                    echo "<p>" . "<strong>" .$Company . "</strong>" . "<br>";
                }
                else {
                    echo "<p>" . "<strong>" . "<a target='blank' href=" .$Website . ">" .$Company . "</a>" . "</strong>" . "<br>";
                }

SQL + Binded code (First/Working one)

$sql="SELECT First_Name, Last_Name, Suffix,  Email, Company, WorkAddress1, WorkCity, WorkStateProvince, WorkZipCode, Work_Phone, Fax, Ancillary, SmallGroup, IndividualPlans, LongTermCare, Medicare, LargeGroup, TPASelfInsured, CertifiedForPPACA, Website FROM `roster` WHERE Last_Name = '".$q."' OR Company = '".$q."' OR WorkCity = '".$q."' OR WorkZipCode = '".$q."' ORDER BY Last_Name ASC";

        if(!$stmt = $con->Prepare($sql))
    { 
        die; 

    }else{
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($First_Name, $Last_Name, $Suffix, $Email, $Company, $WorkAddress1, $WorkCity, $WorkStateProvince, $WorkZipCode, $Work_Phone, $Fax, $Ancillary, $SmallGroup, $IndividualPlans, $LongTermCare, $Medicare, $LargeGroup, $TPASelfInsured, $CertifiedForPPACA, $Website);
        $rows = $stmt->num_rows;

SQL + Binded code (Not working one)

$poststr = $_POST['expertise']; //get our post data
    if(count($poststr) > 1){ //count to make sure we have an array
        $expertise = implode(" AND ",$_POST['expertise']); //implode the array using AND as glue
    }
    else{              //otherwise if it is only one no need for implode
        $expertise = implode("",array($poststr));
    }
    //here is our string for prepared statement
    $sql = "SELECT First_Name, Last_Name, Suffix, Email, Company, WorkAddress1, WorkCity, WorkStateProvince, WorkZipCode, Work_Phone, Fax, Ancillary, SmallGroup, IndividualPlans, LongTermCare, Medicare, LargeGroup, TPASelfInsured, CertifiedForPPACA, Website FROM roster WHERE ".$expertise." = 1 ORDER BY Last_Name ASC";

    if(!$stmt = $con->Prepare($sql))
    { 
        die;

    }else{
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($First_Name, $Last_Name, $Suffix, $Email, $Company, $WorkAddress1, $WorkCity, $WorkStateProvince, $WorkZipCode, $Work_Phone, $Fax, $Ancillary, $SmallGroup, $IndividualPlans, $LongTermCare, $Medicare, $LargeGroup, $TPASelfInsured, $CertifiedForPPACA, $Website);
        $rows = $stmt->num_rows;

Javascript + AJAX (First one/Working one)

<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("bodyA").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("bodyA").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","process.php?q="+str,true);
xmlhttp.send();
}
</script>

jQuery + AJAX (Second one/Not working)

$('input').on('click', function() { //Pulls data based on radial input
    var value = $(this).val();
    $.ajax({
        type: 'POST',
        datatype: "html",
        data: {
            expertise: value
        },
        url: "expertise.php",
        success: function (data) {
            $('#bodyA').html(data);
        }
    });
});

Any idea?

Live Site

  • 写回答

1条回答 默认 最新

  • doutenglou6588 2013-09-30 19:09
    关注
    "<a target='blank' href=" .$Website . ">"
    

    This is your problem: You do not have quotes around your url. It outputs like this:

    <a href=http://whatever.com/path>Company</a>
    

    You need to add quotes like this:

    "<a target='blank' href='" .$Website . "'>"
    

    The url looks like this!

    <a target='blank' href=http://www.samfiorentino.com/>Sam Fiorentino & Associates</a>
    

    It needs quotes. The ending / in the URL is ending the <a>.


    The reason why the first one works but the second one doesn't:

    innerHTML lets the browser interpret the html. $(...) is interpreted by jQuery, which does some fancy things for browser compatibility, but sometimes has drawbacks. Some browsers attempt to fix bad markup, and sometimes the browser does a bad job of it. jQuery makes them all mostly act the same.

    See this jsfiddle for comparison: http://jsfiddle.net/Rk7SQ/

    <p>Browser rendering:</p>
    <p><a target='blank' href=http://www.samfiorentino.com/>Sam Fiorentino & Associates</a></p>
    
    <p>jQuery rendering:</p>
    <p id="jqrender"></p>
    
    $(function() {
        $('#jqrender').html("<a target='blank' href=http://www.samfiorentino.com/>Sam Fiorentino & Associates</a>");
    });
    

    You can see that they are different.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀