1

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

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Josh Powell
  • 6,219
  • 5
  • 31
  • 59
  • 1
    Where are the variables $Company and $Website set? Can we see that code? –  Sep 30 '13 at 19:03
  • and the js/jquery stuff.. – carter Sep 30 '13 at 19:04
  • Sure! I just added an extra line of important info in the fourth paragraph. Let me upload the requested code. – Josh Powell Sep 30 '13 at 19:05
  • provide complete code if possible... – Sunil Kumar Sep 30 '13 at 19:05
  • Won't fix your problem but you can save a lot of keystrokes without having to concatenate every single element such as `echo "

    " . "" .$Company . "" . "
    ";` --- let's shrink that down to `echo "

    " .$Company . "
    ";` etc. --- unless concatenates turn your crank.

    – Funk Forty Niner Sep 30 '13 at 19:05
  • Execute the `SQL`query again between the first and second while loop. – djot Sep 30 '13 at 19:06
  • Are you expecting `$Company` and `$Website` etc to be read from each database fetch? I can't see that they are, presently. – halfer Sep 30 '13 at 19:08
  • 1
    You say one script works and the other doesn't. So sopy paste the first into the second and vice-versa. Now if the first doesn't work and the second does, there is some weird typo somewhere. Otherwise it simply means that in the second context some variable is not set, like you're not getting a correct result for the db. – Sébastien Sep 30 '13 at 19:12
  • The problem is I wrote the first one and then copy and pasted it to the second one. This is why I asked the question because it's the same code. I just am not very good at php :/ – Josh Powell Sep 30 '13 at 19:13
  • @Fred-ii- I will most definitely start doing what you suggested! It won't help me here but it will allow me to write more in the long run while being faster. – Josh Powell Sep 30 '13 at 19:16
  • @Bucky24 The $Company = Company and $Website = Website. They are both just columns in my database. – Josh Powell Sep 30 '13 at 19:30

1 Answers1

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

000
  • 26,951
  • 10
  • 71
  • 101