PHP – Using getElementByID or $_POST

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:

Read More
<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.

Related posts

1 comment

  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.

Comments are closed.