3

I have a WordPress theme which uses a combination of PHP/JS/AJAX to present a booking form.

I am trying to access the value of an output field which is rendered by the following:

<div class="output">
    <p><?php _e('Check-in', 'bookyourtravel') ?></p>
    <p class="step_1_date_from_holder" id="pccfromdate"></p>
    <p><?php if (!empty($accommodation_check_in_time)) { echo ' ' . $accommodation_check_in_time; } ?></p>
</div>

I need to get the value from the pccfromdate text. The variables that I have seen through the booking process at http://epiccloudshare.com/penninelettings/hotel/curlew-yurt-village/ are as follows:

$date_from (set up in a .js file)

$selected_date_from (seen when I do a Chrome "inspect element")

I have tried

<?php $date_from2 = document.getElementByID('pccfromdate').value; ?>

but if my understanding is starting to develop, this is not possible because PHP is server side and JS is client side

The form has a POST method against it so I also tried

<?php if(isset($_POST['selected_date_from']))
    $date_from2 = $_POST['selected_date_from']; ?>

Any pointers would be appreciated

EDIT

I did not indicate that the $_POST attempt did not work either. So, as indicated by machavity I think the answer is something to do with AJAX as the value I require is displayed via the step_1_date_from_holder class. I am able to modify the php script to assign a name and/or id to the <p> but I haven't got a clue as to how to use AJAX to get at the actual value to get it back into a PHP variable.

5
  • One way to do something like this would be to use AJAX. Commented Sep 9, 2015 at 12:31
  • 1
    Sidenote: The function is getElementById() and not getElementByID() it's case-sensitive. developer.mozilla.org/en-US/docs/Web/API/Document/… and you can't inject JS into PHP like that. Commented Sep 9, 2015 at 12:34
  • If you want to access a POST array, then you will need a name attribute for the form element(s). I.e.: name="selected_date_from" Commented Sep 9, 2015 at 12:36
  • possible duplicate of How to get VALUE from FORM without Submitting it? Commented Sep 9, 2015 at 12:41
  • Is 'pccffromdate' a form input (when being posted to the output page)? E.g. input type="text", select etc. Commented Sep 9, 2015 at 12:42

1 Answer 1

1

You are correct that getElementById() is a JavaScript function, and therefore using it inside PHP code will not work.

If you are trying to access a $_POST variable, this is quite simple to do:

  • Observe the form you are working with, and look at the name attribute's value. For instance, name="foo" can be accessed with $_POST['foo'], or;
  • Use print_r($_POST); to echo the $_POST array.

Remember to use something like htmlspecialchars() to escape user input.

Sign up to request clarification or add additional context in comments.

Comments

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.