-2

hi there i got a jquery/ajax/javascript plugin that i'm calling/running directly off my HTML website - it's basically a vertical ticker - news update ticker -

here's the code -

<script src="jquery.vticker-min.js"></script>

        <script type="text/javascript">
    $(function(){
        $('#news-container').vTicker({ 
            speed: 500,
            pause: 3000,
            animation: 'fade',
            mousePause: false,
            showItems: 3
        });
            $('#news-container1').vTicker({
            speed: 700,
            pause: 4000,
            animation: 'fade',
            mousePause: false,
            showItems: 1
        });
    });

</script>

I have a PHP file which prints out statements automatically inserted in a PHP table in the backend which is then automatically printed out on the ticker.

Here's the PHP code for that - the name of the PHP file is "getuser2.php"...

<?php

$con=mysqli_connect("localhost","root","*****","smartliving");

// check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM smartliving.my_dba");

while($row = mysqli_fetch_array($result))
  {
  echo $row['LastName'] . " " . $row['FirstName'];
  echo "<br>";
  }

mysqli_close($con);
?>

the PHP file spits out the updated information in between the following tags (which contain the ticker):

<div style="overflow: hidden; position: relative; height: 125px;" id="news-container">
</div>

without reading from the PHP file, the updated code would be placed in between the two tags above... ...now how do I go about calling the php file from the javascript news update ticker and displaying it between the two div tags in the main HTML page? the javascript news ticker is ALSO called from the main HTML page but in another part of the page...

it seems simple...but its been boggling my mind for a quite a while

ok - so here's what i did - Rob M - it made NO difference what so ever!!!

here's the stuff that i did - updated code and all according to what Rob asked me to do: - i see no difference though!

From the PHP side -

<?php

$con=mysqli_connect("localhost","root","*******","smartliving");

// check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM smartliving.my_dba");

while($row = mysqli_fetch_array($result))
  {
  echo $row['LastName'] . " " . $row['FirstName'];
  echo "<br>";
  }
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $response = array();
    while($row = mysqli_fetch_array($result))
    {
        array_push($response, $row);
    }
    header('Content-type: application/json');
    echo json_encode($response);
    exit;
}
mysqli_close($con);
?>

From the Javascript side:

<script src="jquery.vticker-min.js"></script>

    <script type="text/javascript">
$(function(){
    var $news_container = $('#news-container');
    $.getJSON('getuser2.php', function(data){
            $.each(data, function(item){
                $news_container.append('<div style="overflow: hidden; position: relative; height: 125px;" id="news-container">'+item.LastName+' '+item.FirstName+'</div>');
            });
    });
    $('#news-container').vTicker({ 
        speed: 500,
        pause: 3000,
        animation: 'fade',
        mousePause: false,
        showItems: 3
    });
        $('#news-container1').vTicker({
        speed: 700,
        pause: 4000,
        animation: 'fade',
        mousePause: false,
        showItems: 1
    });
});

From the HTML side:

    <div style="overflow: hidden; position: relative; height: 125px;" id="news-container">
    <ul style="position: absolute; margin: 10pt; padding: 0pt; top: 0px;">



        <li style="margin: 0pt; padding: 0pt; height: 38px; display: list-item;">
            <div>
                4) jugbit.com jquery vticker more info more info more info 
            </div>
        </li>
    <li style="margin: 0pt; padding: 0pt; height: 38px; display: list-item;">
            <div>
                1) Lorem ipsum dolor sit amet, porta at, imperdiet id neque. 
            </div>
        </li><li style="margin: 0pt; padding: 0pt; height: 38px; display: list-item;">
            <div>

                2) Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
            </div>
        </li><li style="margin: 0pt; padding: 0pt; height: 38px;">
            <div>
                3) Lorem ipsum dolor sit amet more info more info more info 
            </div>
        </li></ul>
</div>
</div>

OK...LETS SIMPLIFY THIS ONE STEP AT A TIME - HOW DO I CALL A PHP FILE ONTO A REGULAR HTML FILE ...FORGET THE JAVASCRIPT/JSON PART - THE PHP FILE RETURNS A SET OF STRINGS FROM A DATABASE IN THE BACKEND...I NEED THE STRING TO BE REPRINTED ONTO THE FRONT PAGE OF THE HTML FILE...HOW WOULD I MANAGE TO DO IT? ANY POINTERS?

getuser2.php returns the following which it extracted from an InnoDB databse in the backend:

Griffin Peter
Griffin Lois
Swanson Joseph
Quagmire Glenn

How do i get this list to be printed onto an HTML file? it would be nice if i could know!

Jose Sanchez
  • 7
  • 1
  • 6
  • 2
    AJAX is what you are looking for: http://api.jquery.com/jQuery.ajax/ – Rob M. Nov 21 '13 at 16:22
  • so how do i call it though? – Jose Sanchez Nov 21 '13 at 16:35
  • how do i call the PHP file to spit out its text from the database onto the ticker via AJAX? – Jose Sanchez Nov 21 '13 at 16:36
  • the link u sent me Rob, just confuses matters!!! Help! SOS! – Jose Sanchez Nov 21 '13 at 16:45
  • Ha, read my answer and see if that clarifies things – Rob M. Nov 21 '13 at 16:46
  • lemme try this and see if it works or not – Jose Sanchez Nov 21 '13 at 16:58
  • hey Rob ma man...it don't work! i'll post the code that i have! – Jose Sanchez Nov 21 '13 at 17:18
  • Updated my example, you need to accept my edit to your question, do not post your password here. – Rob M. Nov 21 '13 at 17:31
  • i accepted the edit i think...do u see any updates Rob? – Jose Sanchez Nov 21 '13 at 17:45
  • oh man i'm sooo frustrated - NOTHING'S CHANGED SO FAR!!! The file isn't calling the getuser2.php file which returns the string with first and last name from the database...but getuser2.php is working just fine alright!!! – Jose Sanchez Nov 21 '13 at 17:52
  • I made an edit now Rob...check please! – Jose Sanchez Nov 21 '13 at 17:57
  • ok Rob, I made it simpler for us to understand...forget the Javascript Ticker stuff...how would I print it out in a regular/standard HTML file? got any ideas? please fill me in! Thnx a mill for the help! – Jose Sanchez Nov 21 '13 at 18:00
  • i'd need to print out the info that has been extracted from the database and printed on getuser2.php...any pointers Rob? – Jose Sanchez Nov 21 '13 at 18:01
  • @Jose: following on from my answer to you yesterday, I had a read of a few of your questions. Many of them are too broad for Stack Overflow, and as you have experienced several times, the broadness has caused a lot of assistance to be required in several cases (this question is another one). In turn, this causes you frustration because it looks like people don't want to help - in truth, it is just that people do not want to be answering one question for hours, and/or getting into extended one-to-one discussion. – halfer Jan 22 '15 at 10:18
  • Metaphorically, it seems that you are trying to play a violin concerto before you have taught yourself how to hold the instrument and practice some scales. One possible solution is to get a good book on the topics in question and put in some serious study. There are a lot of tutorials and beginner groups on the web, and I can direct you to some if you like. Also, use a search engine to find what you need: if you want examples of AJAX then try "AJAX PHP example" to start with. Good luck! – halfer Jan 22 '15 at 10:21

1 Answers1

1

As my original comment stated, AJAX is what you are looking for. It is a very important aspect of web development and I suggest you click the link I posted to the jQuery documentation and read the documentation and/or read the Wikipedia article on AJAX.

Essentially, AJAX allows you to fetch content and inject it into the DOM without reloading the page. That really is the answer to the question: 'how to call php file from html/javascript".

Specific examples of how you could use it are below.

On your PHP side, you can detect that it's an AJAX request with the following:

<?php

$con=mysqli_connect("localhost","root","********","smartliving");

// check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM smartliving.my_dba");

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $response = array();
    while($row = mysqli_fetch_array($result))
    {
        array_push($response, $row);
    }
    header('Content-type: application/json');
    echo json_encode($response);
    exit;
} else {
    while($row = mysqli_fetch_array($result))
    {
        echo $row['LastName'] . " " . $row['FirstName'];
        echo "<br>";
    }
}

mysqli_close($con);
?>

In your JavaScript, you can make the request via a nice shortcut method $.getJSON (Note: I'm using the HTML you provided, but please be aware that having multiple elements with the same id is improper and will have unintended consequences, I suggest switching to a class instead):

var $ul = $('#news-container').find('ul');
$.getJSON('getuser2.php', function(data){
    $.each(data, function(item){
       $news_container.append('<li style="margin: 0pt; padding: 0pt; height: 38px;"><div>'+item.LastName+' '+item.FirstName+'</div></li>');
    });
});

This is my best guess with the information you have provided. Happy to elaborate on this if needed.

halfer
  • 19,824
  • 17
  • 99
  • 186
Rob M.
  • 35,491
  • 6
  • 51
  • 50
  • it didn't work! :( made NO difference to the code! – Jose Sanchez Nov 21 '13 at 17:28
  • would you like to see the jquery ticker? jquery.vticker-min.js – Jose Sanchez Nov 21 '13 at 17:29
  • Rob, do you want me to email you the job that i've done so far? the entire website which i did? – Jose Sanchez Nov 21 '13 at 17:30
  • @JoseSanchez Common. That kind of interactive debugging isn't what this site is for. Nobody here wants to do your entire job for you. – user229044 Nov 21 '13 at 17:39
  • i'm not suggesting that meagar...i just need the PHP to be called onto my main ticker...as a string file...then i'm done!!! – Jose Sanchez Nov 21 '13 at 17:43
  • I'm sorry @JoseSanchez, that's as much help as I can provide. This could *should* do what you want it to but I "it doesn't work" isn't really helping. How is not working? What errors are you getting? – Rob M. Nov 21 '13 at 17:48
  • i'm NOT getting any errors...the ticker is displaying stuff that has been written in between the tags...not the strings returned in the getuser2.php file...do ya get what i'm saying? – Jose Sanchez Nov 21 '13 at 17:53
  • ok Rob...lets take this one step at a time - how would I call getuser2.php on a regular html file - getuser2.php only returns a pair of strings from a database list in the backend of the database...forget the stuff about the ticker...how would i call it up and have it print out as a string in a regular HTML file? – Jose Sanchez Nov 21 '13 at 17:55