Ivan
Ivan

Reputation: 5238

PHP get input value without submit

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

Upvotes: 1

Views: 3352

Answers (3)

Emil S. J&#248;rgensen
Emil S. J&#248;rgensen

Reputation: 6366

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")

Upvotes: 1

guest271314
guest271314

Reputation: 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()
})

Upvotes: 1

Han Arantes
Han Arantes

Reputation: 775

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

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

Upvotes: 0

Related Questions