MarioHussein Posted November 21, 2014 Share Posted November 21, 2014 (edited) 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 November 21, 2014 by Ch0cu3r Added code tags Quote Link to comment https://forums.phpfreaks.com/topic/292624-pop-up-windows-in-php/ Share on other sites More sharing options...
cyberRobot Posted November 21, 2014 Share Posted November 21, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/292624-pop-up-windows-in-php/#findComment-1497246 Share on other sites More sharing options...
MarioHussein Posted November 21, 2014 Author Share Posted November 21, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292624-pop-up-windows-in-php/#findComment-1497247 Share on other sites More sharing options...
Solution Ch0cu3r Posted November 21, 2014 Solution Share Posted November 21, 2014 (edited) 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 November 21, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/292624-pop-up-windows-in-php/#findComment-1497255 Share on other sites More sharing options...
MarioHussein Posted November 21, 2014 Author Share Posted November 21, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/292624-pop-up-windows-in-php/#findComment-1497266 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.