PHP函数忽略if语句语句、函数、PHP、if

2023-09-10 20:41:21 作者:我忘了我自己

由于一级代理商希望他的网站的网址上,我曾在一个月前,我最后不得不做出一些细微的变化的功能。我有一个运行一个非常类似的脚本二元函数的PHP页面,但我有一个基于两个值集进行两次。他们所呼应到与AJAX页面是完全一样的,这是它变得有点怪异......

第一个脚本我做成功了,但我需要做一个如果ELSEIF其他语句,这样每个人都剂没有那么去任何地方的链接。摆弄周围这种说法后,我能得到的只是一个代理商有自己的网站的网址在那里。有一次,我做完我下的是IM pression,这将是平滑航行从there..it不...

我用的是完全一样的语句为他们的剧本,只有一个作品。不同于他们的唯一的事情是什么样的价值被接受,而我使用JavaScript + AJAX的第一个(其中一期工程),然后再决定学习jQuery + AJAX做下一个。在此之前,他们所有的工作,这是确切的code为除了使用JavaScript / jQuery的(这是相同的语言)和一个用途 GET ,而另一使用 POST

功能运行时,我也得到任何错误或任何东西。代理的名字是山姆佛罗伦萨是唯一一个与网站的网址。 我走进了控制台,第二次搜索,单选按钮,它显示了锚标记这是问题的根目录以外的公司名称。一个为什么会显示它正确而其他没有按'T?

第一个PHP (工程)

 而($ stmt->取()){//从数据库中获取结果
                回声< D​​IV CLASS ='agentcon'>中。 &所述;跨度类='agentn'>中。 其中&;强>中。将First_Name $。与& NBSP; 。 $姓氏。与& NBSP; 。 $后缀。 < / STRONG>中。 &所述; /跨度>中。 < A HREF =的mailto:,$电子邮件。 >中。 <跨度类=电子邮件>中。 发送电子邮件至。与& NBSP; 。将First_Name $。 &所述; /跨度>中。 &所述; / A>中< D​​IV CLASS ='floathr'>< / DIV>中;
                如果($公司== NULL){
                    回声&其中; P>中;
                }
                ELSEIF($网站== NULL){
                    回声&其中; P>中。 <强>中。$公司。 < / STRONG>中。 < BR>中;
                }
                其他 {
                    回声&其中; P>中。 <强>中。 <目标=空白HREF =$网站。 >中。$公司。 &所述; / A>中。 < / STRONG>中。 < BR>中;
                }
 

二PHP (不工作)

 而($ stmt->取()){//从数据库中获取结果
                回声< D​​IV CLASS ='agentcon'>中。 &所述;跨度类='agentn'>中。 <强>中,将First_Name $。与& NBSP; 。$ LAST_NAME。与& NBSP; 。 $后缀。 < / STRONG>中。 &所述; /跨度>中。 < A HREF =的mailto:,$电子邮件。 >中。 <跨度类=电子邮件>中。 发送电子邮件至。与& NBSP; ,将First_Name $。 &所述; /跨度>中。 &所述; / A>中< D​​IV CLASS ='floathr'>< / DIV>中;
                如果($公司== NULL){
                    回声&其中; P>中;
                }
                ELSEIF($网站== NULL){
                    回声&其中; P>中。 <强>中。$公司。 < / STRONG>中。 < BR>中;
                }
                其他 {
                    回声&其中; P>中。 <强>中。 <目标=空白HREF =$网站。 >中。$公司。 &所述; / A>中。 < / STRONG>中。 < BR>中;
                }
 

SQL +绑定code (头等舱/工作的)

  $ SQL =SELECT名字,姓氏,后缀,电子邮件,公司,WorkAddress1,WorkCity,WorkStateProvince,WorkZip code,Work_Phone,传真,配套,SmallGroup,IndividualPlans,LongTermCare ,医疗保险,LargeGroup,TPASelfInsured,CertifiedForPPACA,网站从`roster` WHERE姓氏='。$ Q。或公司='。$ Q'OR WorkCity ='。$ Q'OR WorkZip code ='$ Q。'ORDER BY姓氏ASC;

        如果(!$语句= $ CON-> prepare($ SQL))
    {
        死;

    }其他{
        $ stmt->执行();
        $ stmt-> store_result();
        $ stmt-> bind_result($的FIRST_NAME:$姓氏,$后缀,$电子邮件,$公司,$ WorkAddress1,$ WorkCity,$ WorkStateProvince,$ WorkZip code,$ Work_Phone,$传真,$辅助,$ SmallGroup, $ IndividualPlans,$ LongTermCare,$医保,$ LargeGroup,$ TPASelfInsured,$ CertifiedForPPACA,$网站);
        $行数= $ stmt-> NUM_ROWS;
 
js中if语句嵌套函数出现问题

SQL +绑定code (不工作的)

  $ poststr = $ _ POST ['专业知识']; //获得我们发布数据
    如果(计数($ poststr)→1){//数,以确保我们有一个数组
        $专业知识=破灭(AND,$ _ POST ['专业知识']); //使用和胶水破灭数组
    }
    其他{//否则,如果它只有一个不需要破灭
        $专业知识=破灭(,数组($ poststr));
    }
    //这里是我们的字符串为prepared声明
    $ SQL =SELECT名字,姓氏,后缀,电子邮件,公司,WorkAddress1,WorkCity,WorkStateProvince,WorkZip code,Work_Phone,传真,配套,SmallGroup,IndividualPlans,LongTermCare,医疗保险,LargeGroup,TPASelfInsured,CertifiedForPPACA,网站从名册WHERE。$的专业知识。= 1 ORDER BY姓氏ASC;

    如果(!$语句= $ CON-> prepare($ SQL))
    {
        死;

    }其他{
        $ stmt->执行();
        $ stmt-> store_result();
        $ stmt-> bind_result($的FIRST_NAME:$姓氏,$后缀,$电子邮件,$公司,$ WorkAddress1,$ WorkCity,$ WorkStateProvince,$ WorkZip code,$ Work_Phone,$传真,$辅助,$ SmallGroup, $ IndividualPlans,$ LongTermCare,$医保,$ LargeGroup,$ TPASelfInsured,$ CertifiedForPPACA,$网站);
        $行数= $ stmt-> NUM_ROWS;
 

的Javascript + AJAX (第一招/工作的)

 <脚本>
功能showUser(STR)
{
如果(STR ==)
  {
  的document.getElementById(bodyA)的innerHTML =。
  返回;
  }
如果(window.XMLHtt prequest)
  {// $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
  XMLHTTP =新XMLHtt prequest();
  }
其他
  {// code对IE6,IE5
  XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
  }
xmlhttp.onreadystatechange =功能()
  {
  如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200)
    {
    的document.getElementById(bodyA)的innerHTML = xmlhttp.responseText。
    }
  }
xmlhttp.open(GET,?process.php Q =+ STR,真正的);
xmlhttp.send();
}
< / SCRIPT>
 

的jQuery + AJAX (第二个/不工作)

  $('输入')。在('点击',函数(){//基于径向输入提取数据
    VAR值= $(本).VAL();
    $阿贾克斯({
        键入:POST,
        数据类型:HTML,
        数据: {
            专长:值
        },
        网址:expertise.php
        成功:功能(数据){
            $('#bodyA')的HTML(数据)。
        }
    });
});
 

你知道吗?所有帮助将大大AP preciated,如果你需要更多的code,那么只要让我知道!

这也是我所使用的PHP与我如此裸露的!唯一一次

直播网站

解决方案

 <目标=空白HREF =$网站。 >中
 

这是你的问题:你不必在你的URL的报价。其输出是这样的:

 < A HREF = HTTP://whatever.com/path>公司< / A>
 

您需要添加引号是这样的:

 <目标=空白HREF ='。$网站。 '>中
 

的URL看起来是这样的!

 <目标=空白HREF = HTTP://www.samfiorentino.com/>山姆蒂诺和放大器;协会和LT; / A>
 

它需要引号。期末 / 在URL中结束了< A>

之所以将第一个作品,但第二个不:

的innerHTML 让浏览器间preT的HTML。 $ (...)是jQuery的,它做一些花哨的东西对浏览器的兼容性PTED除$ P $,但有时也有缺点。有些浏览器尝试修复坏的标记,有时浏览器做它的坏的工作。 jQuery让他们所有的行为大多是相同的。

进行比较看到这个的jsfiddle: http://jsfiddle.net/Rk7SQ/

 < P>浏览器渲染:< / P>
< P><目标=空白HREF = HTTP://www.samfiorentino.com/>山姆蒂诺和放大器;联营&所述; / a取代;&所述; / P>

&其中p为H.; jQuery的渲染:其中; / P>
&其中,P的id =jqrender>&所述; / P>

$(函数(){
    $('#jqrender)HTML(<目标=空白HREF = HTTP://www.samfiorentino.com/>山姆蒂诺和放大器; Associates公司和LT; / A>中)。
});
 

您可以看到,它们是不同的。

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? All help will be greatly appreciated and if you need more code then just let me know!

This is also the only time I have ever used PHP so bare with me!

Live Site

解决方案

"<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.