0

I made an HTML form inside the Pages section of WordPress, but I need to use the PHP timestamp to submit a hidden field of the current time.

How can I do this? Do I use the "span" tag or something? I think that's for javascript. I just need to be able to submit a timestamp in a hidden file though, so maybe I should use javascript?

Dave Liu
  • 906
  • 1
  • 11
  • 31
  • Can you print anything in PHP in the current page? if so, just feel free to print your hidden input in such a way: `echo "";`. In any case, shouldn't it be easier to get the timestamp **once the form gets submitted?** You're going to call a PHP script in any case, so... – briosheje Aug 07 '14 at 16:26
  • It gets submitted to vanillasoft.com and gets processed in their databases for business. I can change what I want my ID to be, but I'm not sure if I can include a timestamp on the site. The PHP in the WordPress textbox does not get interpreted. I'm thinking about just trying Javascript. – Dave Liu Aug 07 '14 at 16:31
  • Oh, so you definetly can't process it like you want to. In such a case, then, how is your submit form structured? do you have an handler when it is submitted? I suppose that you can edit the current HTML page, right? – briosheje Aug 07 '14 at 16:32
  • Yeah, there's this handler on the site, but it can only configure format, not the actual timestamp :-/ – Dave Liu Aug 07 '14 at 16:44
  • So let me understand: in this handler, can you actually use any php code or are you forced to use html and javascript? just to understand what is the best for your case. – briosheje Aug 07 '14 at 16:50
  • I'm forced to use HTML/JS – Dave Liu Aug 07 '14 at 16:52

2 Answers2

3

Try something like this, if your form is defined in a WordPress template:

<input type="hidden" id="timestamp" value="<?php echo time();?>" />

That will output the timestamp in epoch format. If you want a formatted value, use this for 24 hour formatting:

<input type="hidden" id="timestamp" value="<?php echo date('H:i:s');?>" />

(see http://php.net/manual/en/function.date.php for information regarding formatting options)

I have to agree with briosheje, however: you'd typically want to read the time on the server, after the form has been posted. If not, keep in mind the value that's posted will be stale by a few seconds, possibly minutes (possibly more!)

Adam M
  • 66
  • 4
  • http://support.vanillasoft.com/entries/21084807-Format-for-Incoming-Web-Leads- It seems to only accept XML, so that stinks. – Dave Liu Aug 07 '14 at 16:48
  • The big question, as I said above, is: can he actually use ANY php code? – briosheje Aug 07 '14 at 16:48
  • I know that I can use JS in the text area, so maybe I'll just implement the same, except in JS. Yeah, I just need day, month, year that the information is submitted, so even if it's a few hours late, it should be ok. – Dave Liu Aug 07 '14 at 16:51
  • He mentioned he's using WordPress. If his form is in a template then he can use PHP. If it's in a post or page then there are ways to achieve this: http://stackoverflow.com/questions/18896146/insert-php-code-in-wordpress-page-and-post – Adam M Aug 07 '14 at 16:57
  • DaveL, doesn't VanillaSoft add a date and timestamp automatically when you add leads? Regardless, it looks like you will need to do some server-side PHP development in order to convert the form post content into an XML document. – Adam M Aug 07 '14 at 17:01
1

Wordpress will not parse PHP code. There are plugins that can do it if you desire, but they are less than optimal. Instead, try something like this...

In your functions.php file:

add_shortcode('timestamp', 'my_add_timestamp');

function my_add_timestamp($atts){
    return time();
}

In the page with your form:

<input type="hidden" id="timestamp" value="[timestamp]" />

This will tell WordPress to parse the content of your page/post and replace the [timestamp] with the function call.

BA_Webimax
  • 2,714
  • 1
  • 13
  • 15
  • That's a good solution. @DaveL: if you can't even edit your functions.php file, then check this: http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript and use javascript, that's the only solution left, as far as I know :). To be more generic, check the javascript **date** object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date and use it **in the way you need it to be used** ;) – briosheje Aug 07 '14 at 16:53
  • ok, thank you for this. I'm a novice to WordPress, but this seems like exactly what I'm looking to do. – Dave Liu Aug 07 '14 at 16:56