Jump to content

Pop up windows in PHP


MarioHussein
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi,

 

I have a .txt file with data in the following way:

 

FRENEMY : a person with whom one is friendly despite a fundamental dislike or rivalry :

There seems to be a frenemy relationship between John and Andrew.

PORTMANTEAU : a word or morpheme whose form and meaning are derived from a blending of two or more distinct forms (as smog from smoke and fog) : Lewis Carroll's Jabberwocky is loaded with portmanteau words.

 

The following is my PHP:

<?php

//Check your text file exits
  if (file_exists('interesting_words.txt')) {    

//Retrieve content of text file
  $content = file_get_contents('interesting_words.txt', true);
  //Add every line of file to array
  $lines = explode("\n", $content);
 
//Cycle through all line in file
  $words = array();
  foreach ($lines as $line) {

//Split word and description by :
  list($word, $description, $example) = explode(':', $line);
 
//Create a words array to hold word and description
  $words[] = array('word' => ucfirst(trim($word)), 'description' => ucfirst(trim($description)), 'example' => ucfirst(trim($example)));
}
 
//Generate random number from date
 
//Remove to generate random word every page load
  srand(date("ymd"));
 
//Use random number from 0 to the cound of words array to pick word of the day
  $word_of_the_day = $words[rand(0, (count($words) -1))];
 
//Use word, descriptionand example keys to print the results.
  echo '<span id="word">'.$word_of_the_day['word'] . '</span>'.'<br>'.'<br>';
  echo '<span id="description">'.$word_of_the_day['description'].'</span>'.'<br>'.'<br>';
  echo '<span id="example">'.$word_of_the_day['example'].'</span>'.'<br>'.'<br>';
 }
 
?>

I would like to have it displayed in the following way:

 

FRENEMY

a person with whom one is friendly despite a fundamental dislike or rivalry.

 

BUTTON

 

One would click the button to open a new popup window to show example sentences.

 

My problem is how do I go about coding for the window part.

 

All help appreciated.

Many thanks

Edited by Ch0cu3r
Added code tags
Link to comment
Share on other sites

The pop-up window is actually done on the client side. You could use something like JavaScript to create the pop-up window. Perhaps you could find something in the following Google search:

https://www.google.com/search?q=html%20pop-up%20window

I've got the javascript for the popup window. What I would like to do is get the pop up window show only the corresponding example sentence when the button is clicked. I think this is done using ECHO but i am not quite sure how to do it.

Link to comment
Share on other sites

  • Solution

You will need to pass the word and the example sentence to your script and then echo the sentence.

 

Untested code

echo '<span id="word">'.$word_of_the_day['word'] . '</span>'.'<br>'.'<br>';
echo '<span id="description">'.$word_of_the_day['description'].'</span>'.'<br>'.'<br>';
echo '
  <form method="post" action="example_sentence.php" target="_blank">
    <input type="hidden" name="word" value="'.$word_of_the_day['word'].'" />
    <input type="hidden" name="sentence" value="'.$word_of_the_day['example'].'" />
    <input type="submit" value="See Example" />
  </form>';

Now in example_sentence.php you could do

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    echo '<h1>' . $_POST['word'] . '</h1>';
    echo '<p>' . $_POST['sentence'] . '</p>';
}
else
{
    echo 'Invalid request';
} 

Please wrap code in


tags or click the <> button in the editor when posting code.

Edited by Ch0cu3r
Link to comment
Share on other sites

You will need to pass the word and the example sentence to your script and then echo the sentence.

 

Untested code

echo '<span id="word">'.$word_of_the_day['word'] . '</span>'.'<br>'.'<br>';
echo '<span id="description">'.$word_of_the_day['description'].'</span>'.'<br>'.'<br>';
echo '
  <form method="post" action="example_sentence.php" target="_blank">
    <input type="hidden" name="word" value="'.$word_of_the_day['word'].'" />
    <input type="hidden" name="sentence" value="'.$word_of_the_day['example'].'" />
    <input type="submit" value="See Example" />
  </form>';

Now in example_sentence.php you could do

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    echo '<h1>' . $_POST['word'] . '</h1>';
    echo '<p>' . $_POST['sentence'] . '</p>';
}
else
{
    echo 'Invalid request';
} 

Please wrap code in


tags or click the <> button in the editor when posting code.

Thanks a million Ch0cu3r.

 

Works just as I wanted. Now I'll get down to trying to make the form open in a popup window.

 

Many thanks once again.

 

Mario

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.