0

I have a page (parent) that when a button (with .click funciton) is clicked it uses jQuery .load to load a form into the div. On the parent page is the following PHP

<?php
   $SubmitDATETIME = date("n/j/Y g:i A");
   $timestamp = date ("n/Y");
?>

the "loaded" content uses a remote script to validate the form, but I cannot seem to pass the $SubmitDATETIME and $timestamp to the loaded form. I've tried just using input boxes like

<input type"text" value="<?php echo $SubmitDATETIME ?>"> but they are empty. When I put same code on parent page it works. Should I a) create this function with JS and apply to the button's click funciton, or b)is there a way to pass the $SubmitDATETIME and $timestamp variables from the parent page to the loaded content? possibly make it a remote PHP function and use .get to echo the values? I'm stumped.

thx

***************** EDIT *****************

I might not have been as clear as I needed to be on this, when you click the "button" it invokes a click function $("#singleContent").load('pForm.php #formContent', function() {...}); inside the function is the validation for the form, which contains the submtiHandler. In the submitHandler is a function to build an xml string of all the values of the form. One of which was to "echo the submitDATETIME" php function. this is where i was having issues. When I included the function as a $_SESSION, i could get it to print to the loaded form, but the validation was not echoing it. I know that I could use JS to build the funciton in the validation, but I don't know how. I was able to echo the submitDATETIME to a hidden input and get it's value from that in the xml string. phew. Is there a better way? how would I build the datetime with JS?

2
  • 1
    Javascript has its own date API: w3schools.com/jsref/jsref_obj_date.asp .. why can't you use that? Commented Mar 6, 2011 at 18:31
  • can't get it to format the same way, has to be formatted the same way for backend WCF service. pls help. Commented Mar 6, 2011 at 19:38

3 Answers 3

1

You probably need to put those variables to the $_SESSION array.

on the parent page:

<?php
   $SubmitDATETIME = date("n/j/Y g:i A");
   $timestamp = date ("n/Y");
   session_start();
   $_SESSION['timestamp'] = $timestamp;
   $_SESSION['submitDATETIME'] = $SubmitDATETIME;
?>

in the ajax loaded form:

<?php session_start(); ?>
<input type"text" value="<?php echo $_SESSION['submitDATETIME']  ?>">
Sign up to request clarification or add additional context in comments.

3 Comments

you got me going in right direction, had to echo "$_SESSION['submitDATETIME'] into a hidden input and add the value to the click string like <DATETIME>" + $('[name=date]').val() + "</DATETIME> I guess because the validation was loaded with the click function the value was getting lost?
I don't really know where it's lost. I would have to see a little bit more of your code.
here is a snippet of the click function, which is called when the content is loaded. ... '<DATETIME><?php echo $_SESSION['submitDATETIME'] ?></DATETIME>'... I have the parent page and loaded page as you have above. The only way I can get it to work is to have a hidden input (name=date) with the vale and then in the click funciton "<DATETIME>" + $('[name=date]').val() + "</DATETIME>"
0

make sure input is paced after php code, rest every thing is fine

Comments

0

If the page is already loaded, then you must inform Javascript of the variable sent by PHP. The following code uses PHP to output date values into Javascript strings that can be used later on the page with Javascript.

// Javascript with embedded PHP:
var submitDATETIME = "<?php echo date("n/j/Y g:i A"); ?>";
var timestamp = "<?php echo date("n/Y"); ?>";
var someOtherString = "<?php echo "hello world from PHP";?>";

Right now, these values will both be plain strings in Javascript. If you need to make them into true Date() objects, see the Javascript Date() reference.

Now, as a commenter said above, Javascript's own Date() object will do these same things. But if you need to pass other strings from PHP into Javascript, this will work.

1 Comment

can't get the JS DATE() to format in the correct order. sucks. Pls see EDIT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.