Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8073765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:26:24+00:00 2026-06-05T14:26:24+00:00

I’ve got a simple PHP script which reads specific phrases from a text file

  • 0

I’ve got a simple PHP script which reads specific phrases from a text file and displays one at random on the web page. In order to display another random phrase the whole page has to be refreshed at present. My question is – Is there a way to update just the area of the page which contains the phrase, displaying a new phrase each time a button is clicked? Currently the PHP below is contained in a h1 tag which has been styled using CSS. I then include a comment underneath it telling the user to refresh the page to show another random phrase. Ideally I would like this comment to be changed to a button so the user does not have to refresh the whole page each time.

Here is the simple PHP used to display a random phrase:

$str = file_get_contents('words.txt'); //Take the contents from the file to the variable
$result = explode(',',$str); //Split it by ','
echo $result[array_rand($result)]; //Return a random entry from the array.

Any help would be greatly appreciated.

Cheers, Tom

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:26:26+00:00Added an answer on June 5, 2026 at 2:26 pm

    Example I used to get started learning PHP/AJX, excerpt from Using jQuery…

    Create a php script to receive http requests and fetch data from the database

    //--------------------------------------------------------------------------
    // Example php script for fetching data from mysql database
    //--------------------------------------------------------------------------
    $host = "localhost";
    $user = "root";
    $pass = "root";
    
    $databaseName = "ajax01";
    $tableName = "variables";
    
    //--------------------------------------------------------------------------
    // 1) Connect to mysql database
    //--------------------------------------------------------------------------
    include 'DB.php';
    $con = mysql_connect($host,$user,$pass);
    $dbs = mysql_select_db($databaseName, $con);
    
    //--------------------------------------------------------------------------
    // 2) Query database for data
    //--------------------------------------------------------------------------
    $result = mysql_query("SELECT * FROM $tableName");          //query
    $array = mysql_fetch_row($result);                          //fetch result    
    
    //--------------------------------------------------------------------------
    // 3) echo result as json 
    //--------------------------------------------------------------------------
      echo json_encode($array);
    

    Client example

    Output:

    this element will be accessed by jquery and this text replaced

    <script id="source" language="javascript" type="text/javascript">
    
    $(function () 
    {
      //-----------------------------------------------------------------------
      // 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
      //-----------------------------------------------------------------------
      $.ajax({                                      
        url: 'api.php',                  //the script to call to get data          
        data: "",                        //you can insert url argumnets here to pass to api.php
                                         //for example "id=5&parent=6"
        dataType: 'json',                //data format      
        success: function(data)          //on recieve of reply
        {
          var id = data[0];              //get id
          var vname = data[1];           //get name
          //--------------------------------------------------------------------
          // 3) Update html content
          //--------------------------------------------------------------------
          $('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname); //Set output element html
          //recommend reading up on jquery selectors they are awesome 
          // http://api.jquery.com/category/selectors/
        } 
      });
    }); 
    
    </script>
    </body>
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.