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

11
  • 1
    Where are the variables $Company and $Website set? Can we see that code? Commented Sep 30, 2013 at 19:03
  • and the js/jquery stuff.. Commented Sep 30, 2013 at 19:04
  • Sure! I just added an extra line of important info in the fourth paragraph. Let me upload the requested code. Commented Sep 30, 2013 at 19:05
  • provide complete code if possible... Commented Sep 30, 2013 at 19:05
  • 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. Commented Sep 30, 2013 at 19:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

17 Comments

I added that after and was stated in the comments.
I will give this a try but why do you think it works with one while not the other? it also works with the email link which I did not add quotes on it as well.
Thank you for correcting me and I'll try to figure out what is going on. My last question then is why does this work just fine with the email link? is it because it uses the <a href="mailto:[email protected]"> This is just all slightly confusing to me but that's why I'll have a couple classes on all of this soon.
Ahh yes I wasn't thinking about the / in the url still. Thanks for your help, once I get the function working I will check this as the right answer.
I figured out what was going on and why nothing was changing. I did not re upload my scripts.js file to the server to replace previous attempts from me. It was running off a GET value while my script was looking for a POST value. Thanks a ton mate and I greatly appreciate the time you spent helping me.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.