0

I am a pure beginner of Javascript. I got some advanced experience with php but until now I refused to work with JS.

So now I have a question. I wanted to make possible to pickup date-value from a php-generated calendar and put it into a form input field just below of the same page / script.

My idea was to create an onlick-event for each date data. So in my calendar I have this:

$date = date('Y-m-d',$daystamp);
$post = '<div class="cal_day"><a href="#" onclick="dateclick($date)">'.$day.'</a></div>';

What I thought to do with Javascript is the following:

function dateclick(var x)
{
document.getElementById("date_entry").innerHTML = var x;
}

And my html-input field where I like to have the choosen date-values:

<input type="text" id="date_entry" name="abs_date" size="15" max-length="15"/>

I am sure the solution is simple. At W3Schools I saw that the output of JS doesn't require a return all the time? Should I add it?

Thank you for any answer or help.

UPDATE: So I tried what you mentionned as solution/s: - I removed the "var" before the x. - I have encapsed the $date.

It is still not working. When I look up the element with OPERA it tells me it is a event handler but source file missing. But I have included the functions.js and another function in the same file is working well. I tried the same thing in the try-field on W3Schools and it worket there.. So I guess that there is something in my structure around it that isn't working.

The calendar source line is included above the form element. Is this a problem?

I don't know if I zapped something.

GOT IT The problem was that I needet to put the value into an input element.

The following code works:

function dateclick(x)
{
document.getElementById('date_entry').value=x;
return false;
}

Thanks for all your help!

5
  • which link at W3Schools? Commented Jul 4, 2014 at 13:41
  • It would help a lot if you told us what exactly is your problem. What should happen, what does happen, what errors do you get... Commented Jul 4, 2014 at 13:41
  • You're missing 's around $date in the onclick. But since you are within a '-enclosed string, you'll need to escape them: $post = '<div class="cal_day"><a href="#" onclick="dateclick(\'$date\')">'.$day.'</a></div>'; Commented Jul 4, 2014 at 13:42
  • And of course, you need to remove the var in your assignment: document.getElementById("date_entry").innerHTML = x; Commented Jul 4, 2014 at 13:43
  • I tried with x (removed var). It doesn't want to work! Can I do some var_dump to verify if x is really filled with my date variable? Commented Jul 4, 2014 at 13:55

2 Answers 2

1

Your php should be:

$date = date('Y-m-d',$daystamp);
$post = '<div class="cal_day"><a href="#" onclick="dateclick(\''. $date. '\')">'.$day.'</a></div>';

And you javascript should be:

function dateclick(x)
{
   document.getElementById("date_entry").innerHTML = x;
   return false; // This will force the browser not to load the url.
}
Sign up to request clarification or add additional context in comments.

Comments

0

You don't have to go through all that trouble, this should work:

<script>
    var phpDate = "<?php echo $date; ?>";
</script>

2 Comments

He's trying to do a date picker that will insert a date into an input field based on what the user clicks. At least that's what I understood.
Yes. That wouldn't help me here - the date in the calendar is variable so I need to get the value of the date generated by PHP.

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.