Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Closed Thread
 
Thread Tools Display Modes
Share |
  #1  
Old 06-07-2014, 11:22 AM
ConduciveMammal's Avatar
ConduciveMammal ConduciveMammal is offline
Contributing Member
 
Join Date: 03-28-13
Location: Walsall, UK
Posts: 266
iTrader: 0 / 0%
Random textfield placeholder

I'd like to have a textfield with a placeholder and each time the page is loaded, I'd like it to display a different placeholder, maybe read each one from a file and display each line on a random sequence whenever the page loads.

Does anyone know how I can do this? Preferably within WordPress.

Thanks.
 

Advertisement

Advertisement

  #2  
Old 06-08-2014, 09:05 AM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
There's lots of scripts for random quotes - but it doesn't have to be quotes, the text could be anything.

This guy went on a search for something that worked well in WP and modified some code he found - see if this will work for you : http://madebyknight.com/wordpress-random-quote/
 
  #3  
Old 06-08-2014, 09:21 AM
ConduciveMammal's Avatar
ConduciveMammal ConduciveMammal is offline
Contributing Member
 
Join Date: 03-28-13
Location: Walsall, UK
Posts: 266
iTrader: 0 / 0%
Quote:
Originally Posted by Katt View Post
There's lots of scripts for random quotes - but it doesn't have to be quotes, the text could be anything. This guy went on a search for something that worked well in WP and modified some code he found - see if this will work for you : http://madebyknight.com/wordpress-random-quote/
Thanks so much for that, Katt. This is practically perfect for what I need. So, assuming I followed these instructions (but modified it for my needs) would I use this code to generate it as a placeholder:

Code:
<input type="textarea" placeholder="'random_quote'" </input>
The "'random_quote'" being the little php snippet from his code.

Would that work, do you know?

Thanks again for find this for me.
 
  #4  
Old 06-08-2014, 10:09 AM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
No sorry - that won't work. You'll need to use php to pull the quote from the text file. Using the example on that site, this is what you'll put on your page to pull the quote (or whatever text) in :

PHP Code:
<?php random_phrase (); ?>
 
  #5  
Old 06-08-2014, 10:15 AM
ConduciveMammal's Avatar
ConduciveMammal ConduciveMammal is offline
Contributing Member
 
Join Date: 03-28-13
Location: Walsall, UK
Posts: 266
iTrader: 0 / 0%
Quote:
Originally Posted by Katt View Post
No sorry - that won't work. You'll need to use php to pull the quote from the text file. Using the example on that site, this is what you'll put on your page to pull the quote (or whatever text) in : <?php random_phrase (); ?>
Ohh okay, so I would put that php code inside the placeholder tag? Forgive me, I'm quite new to the world of php
 
  #6  
Old 06-08-2014, 10:38 AM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
No need to apologize - we're all here to learn! I've been at this for years, and while I'm better at Windows languages, I have a bit of php too, but I'm always learning something new!! BUT I've never used Wordpress, so when talking about WHERE to look to put some of this code - I have to defer to someone else with WP experience.

I've never tried this code, so I'm only going by what the guy presented in his examples. You don't need the input tag, unless you're doing something there I can't quite understand (if so, a link to a page where you're trying to put this might help)

Note that this guy starts off talking about how to pull from a text file, but about halfway through the article he switches gears to pull the random text from a WP page - his reasoning was that this makes it easier to manage the random text. If you're ok with pulling from a plain text file, you can ignore the last half of that page.

The guy does kind of switch gears a few times, so let me try to simplify what he's saying :

Add this code to functions.php (here's a page that explains how to add code to functions.php - http://wordpress.org/support/topic/h...in-functionphp )

PHP Code:
if( ! function_exists('random_quote')) :
    function 
random_quote() {
        
$quotes file ("wp-content/quotes.txt");
        
$num rand (0, (intval(count($quotes))-1));
        echo 
$quotes[$num];
    }
endif; 
//ends check for random_quote() 
Add this to your stylesheet (with whatever styling you want on the random text) :
Code:
#quote { text-align: center; }
Then on the page where you want the text to appear, put this code :

Code:
<div id="quote"><?php random_quote (); ?></div>
The div is really just to apply the styling, which I would assume you want to do.

Of course don't forget the quotes.txt file in the folder wp-content. If you place it in a different location, don't forget to modify the location in the php code above.
 
  #7  
Old 06-08-2014, 11:33 AM
ConduciveMammal's Avatar
ConduciveMammal ConduciveMammal is offline
Contributing Member
 
Join Date: 03-28-13
Location: Walsall, UK
Posts: 266
iTrader: 0 / 0%
Quote:
Originally Posted by Katt View Post
No need to apologize - we're all here to learn! I've been at this for years, and while I'm better at Windows languages, I have a bit of php too, but I'm always learning something new!! BUT I've never used Wordpress, so when talking about WHERE to look to put some of this code - I have to defer to someone else with WP experience.

I've never tried this code, so I'm only going by what the guy presented in his examples. You don't need the input tag, unless you're doing something there I can't quite understand (if so, a link to a page where you're trying to put this might help)

Note that this guy starts off talking about how to pull from a text file, but about halfway through the article he switches gears to pull the random text from a WP page - his reasoning was that this makes it easier to manage the random text. If you're ok with pulling from a plain text file, you can ignore the last half of that page.

The guy does kind of switch gears a few times, so let me try to simplify what he's saying :

Add this code to functions.php (here's a page that explains how to add code to functions.php - http://wordpress.org/support/topic/h...in-functionphp )

PHP Code:
if( ! function_exists('random_quote')) :
    function 
random_quote() {
        
$quotes file ("wp-content/quotes.txt");
        
$num rand (0, (intval(count($quotes))-1));
        echo 
$quotes[$num];
    }
endif; 
//ends check for random_quote() 
Add this to your stylesheet (with whatever styling you want on the random text) :
Code:
#quote { text-align: center; }
Then on the page where you want the text to appear, put this code :

Code:
<div id="quote"><?php random_quote (); ?></div>
The div is really just to apply the styling, which I would assume you want to do.

Of course don't forget the quotes.txt file in the folder wp-content. If you place it in a different location, don't forget to modify the location in the php code above.
Well, it's good to not have people frustrated at my "newbie-ness" hah

Okay, well to show you what I'm looking for, if you go to this link, you'll see a very basic HTML form, the last textarea box shows the placeholder inside of it, it's this that I want to randomise. Except within a WordPress form, so I would assume, even in WordPress, the same HTML form-element tags would apply or does that have an entirely different code method to build forms?

I'm going to set up a new WordPress website just so I can try the code that you've helped me with, so I can get the gist of things.

Thanks so much for your continued assistance.
 
  #8  
Old 06-08-2014, 04:57 PM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
Ok I'm wading into brand new territory now LOL

I know we can use a php variable in as a text input value, so maybe it will work on a textfield placeholder - but the only variable we have is for the entire random text txt file. The function you put in functions.php is to randomize the text and only pull one line, so we need to put a function in there. I have no idea if that's even possible and a quick google didn't give me any answers.

Two ways to try this - these both need the php code above added to the functions.php.

The first will pull in the entire quotes.txt file - it might be a starting point if it works but the 2nd way fails.

I think this will work because it's just calling a php variable.

Code:
<div id="quote"> <textarea name="comments" placeholder="<?php echo $quotes; ?>" maxlength="1000" cols="25" rows="6"></textarea> </div>

This code will run the php function to randomize and pull one quote - I think - I have no clue whether this will actually work - maybe we'll get lucky with the code, or maybe it will inspire us to find another way LOL

Code:
<div id="quote"> <textarea name="comments" placeholder="<?php random_quote (); ?>" maxlength="1000" cols="25" rows="6"></textarea> </div>
Either way, since you're working with a form, I'd suggest looking into providing some protection against xss before you go live with it.

Last edited by Katt; 06-08-2014 at 04:57 PM. Reason: forgot to close a tag!!
 
  #9  
Old 06-09-2014, 05:04 AM
ScriptMan's Avatar
ScriptMan ScriptMan is online now
Super Moderator
 
Join Date: 02-10-07
Location: Central Kentucky
Posts: 14,015
iTrader: 4 / 100%
I'm not up to writing even pseudo code this morning but I think the Switch function http://www.php.net//manual/en/contro...res.switch.php could be used if the format of the text file was correct. I did such a thing with images once upon a time.
 
  #10  
Old 06-09-2014, 07:47 AM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
Hey cool - most of the examples there use case with switch, but I found one (so far - didn't read the whole page) that uses a randomizer that we might be able to modify for this purpose - running late for work now but I'll try to look at it tonight.

http://www.php.net//manual/en/contro...itch.php#88643
 
  #11  
Old 06-09-2014, 01:31 PM
ScriptMan's Avatar
ScriptMan ScriptMan is online now
Super Moderator
 
Join Date: 02-10-07
Location: Central Kentucky
Posts: 14,015
iTrader: 4 / 100%
I looked and can not easily find the files on my hard drive. If my old memory serves me correctly, a big IF, I used random() to select the number that powered the switch. Random was limited to the number of available choices.

With random you sometimes get repeats unless you want to write some complicated code that excludes the previous choice.
 
  #12  
Old 06-10-2014, 07:07 AM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
I had a looooong Monday - got home late and went straight to bed. I'll try again tonight.

It would be nice if the OP would post back whether either of the attempts I posted above gave any promising results. I'd think the variable worked, but have doubts about the function. Assuming the variable worked, I think the switch will too, just need to tinker with it a bit.
 
  #13  
Old 06-14-2014, 08:51 AM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
Sorry, I've been sick all week but I'm finally starting to feel a little better.

Have you found something that works, or should I try to modify the code Scriptman suggested?
 
  #14  
Old 06-15-2014, 01:12 PM
ConduciveMammal's Avatar
ConduciveMammal ConduciveMammal is offline
Contributing Member
 
Join Date: 03-28-13
Location: Walsall, UK
Posts: 266
iTrader: 0 / 0%
Hey guys,
I'm ever so sorry about the total silence, I'm having an insane amount of trouble with my host so I'm in the process of moving my site to my own reseller host.

Thanks a lot for all of your work and undos and I'll try them out as soon as I get everything fixed.

Thanks again
 
Go Back   Webmaster Forum > Web Development > Coding Forum

Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Cross-Browser Support for HTML5 Placeholder Text in Forms? hirewebdesigner Coding Forum 2 06-21-2012 01:27 AM
Sum of table rows will appear in a textfield newphpcoder Coding Forum 1 12-14-2010 11:25 PM
[Easy] What is the right code for join this 2 textfield basketmen Coding Forum 1 03-22-2007 08:52 AM
"Random non-random" image generator tuckerkatt Coding Forum 2 10-31-2006 06:20 PM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 02:36 PM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2018 VIX-WomensForum LLC