1

Guys i have foreach loop where i list some prices. All price have own input radio button.

By default only one price from looping is checked. I want to get that value when page is lodaed.

I have one session where i store number of days. Based on these days I get the price of cars.

$numDays = $_SESSION['days']; // 5
$calculate_km = $numDays * 140;  // 5*140km

So in page where i want to show total KM i use:

if(isset($_SESSION['days']) {
    $numDays = $_SESSION['days'];
}else {
    // Show default selected radio button value
}

Problem is bcs price list with radio is on the some page and there is no sumbiting

My loop:

<?php if($prices = getCarPricePeriod($car->ID, $od, $do)):?>
    <?php $first = true; ?>
        <?php foreach ($prices as $price): ?>
            <tr>
                <td><input type="radio" name="price" value="<?= $price['value'];?>" <?= $first ? 'checked' : '' ?>> </td>
                <td><input type="text" name="price_name" value=" <?= $price['name'];?>">&euro;/per day</td>
            </tr>
        <?php endforeach; ?>
    <?php $first = false; ?>
<?php endif; ?>

Total KM : <span> <?= $numDays * 140 ;?> </span>

Only is posible to get that value if i submit that form. So i need js for this or any way to do this in php

14
  • Where you want to use it? Commented Mar 2, 2016 at 15:55
  • On the some page where is radio btn Commented Mar 2, 2016 at 15:57
  • Please discribe more the context I want to get that value when page is lodaed (then when you will get it what you want to do?) the context is not clear. Commented Mar 2, 2016 at 16:00
  • Just a second i will update my question. Commented Mar 2, 2016 at 16:02
  • 1
    @Zend "If SESSION['days'] exist set session value. If not exist get value from selected radio button with js. How to put execute that js in that variable if session is not definded" What do you mean by "get value" ? Resend value back to php ? Could you achieve this in php if(isset($_SESSION['days']) { $numDays = $_SESSION['days']; }else { $numDays = $price['name'] } ? Commented Mar 2, 2016 at 16:20

3 Answers 3

1

I want to get that value when page is lodaed.

Use .ready() , selector $("tr input:checked") , .val()

$(document).ready(function() {
  var val = $("tr input:checked").val()
})
Sign up to request clarification or add additional context in comments.

Comments

1

If what you are asking is "how to send data from client-side to server-side", then the answer is AJAX.

Take a look at W3School's AJAX tutorial here: http://www.w3schools.com/ajax/

EDIT 1

for javascript, this should do it:

document.querySelector("input[type=radio]:checked").getAttribute("value")

1 Comment

I dont want to send data i need just value for other calculation.
0

Without submit, you need JavaScript to do that, like this:

<script>
    var p = document.getElementByName("price");
</script>

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.