I am working in view file of opencart. I have 2 text boxes which set date and time respectively. I have a checkbox, its value is 1 when checked and 0 when not checked.What I want is that date and time fields should be hidden if above check box is checked (Done till now).If checkbox is unchecked, show these fields as they are now(Done till now).
Current code:
HTML
<tr id="row_ship_date">
<td>
Shipment Date:
</td>
<td>
<input type="text" id="ship_date" name="ship_date" class="date" onchange="getServices();" />
</td>
</tr>
<tr id="row_ship_time">
<td>
Shipment Time:
</td>
<td>
<input type="text" id="ship_time" name="ship_time" class="time" />
</td>
</tr>
<?php
if (isset($current_date_shipment) && $current_date_shipment == 1) // check box value check{?>
<script type="text/javascript">
document.getElementById('row_ship_date').style.display='none';
document.getElementById('row_ship_time').style.display='none';
getServices();
</script>
<?php }
?>
JS:
function getServices() {
alert('test');
var ship_date = $('#ship_date').val();
var ship_time = $('#ship_time').val();
// code so on..
}
Current issues
1) I am unable to call getServices() if checkbox check is true (php check of if statement) because if its false it gets call onchange event of date and works fine.
2) how can I set current date and current time respectively for both text fields if this function is called under if statement (when both fields are hidden), something like:
function getServices() {
alert('test');
var ship_date = new Date();
var ship_time = currentTime();
// code so on..
}
The problem may be because you are trying to call the function before declaring it.
Try to wrap you code inside
<script>
// self executing function
(function() {
// put your code here
})();
</script>
i.e;
<?php
if (isset($current_date_shipment) && $current_date_shipment == 1) // check box value check
{?>
<script type="text/javascript">
(function() {
document.getElementById('row_ship_date').style.display='none';
document.getElementById('row_ship_time').style.display='none';
getServices();
})();
</script>
try this:
<?php
if (isset($current_date_shipment) && $current_date_shipment == 1) {
$now = new DateTime();
$date = $now->format('Y-m-d');
$time = $now->format('H:i');
?>
<script type="text/javascript">
$('#row_ship_date,#row_ship_time').hide();
$('#ship_date').val(<?php echo $date; ?>);// this should set date value,Use appropriate PHP function.
$('#ship_time').val(<?php echo $time; ?>); // this should set time value,Use appropriate PHP function.
getServices();
</script>
<?php } ?>
You dont need hidden fields to set any value but can set as shown above using the jQuery .val().
Just replace the "new date/time value here strings" with PHP variables containing the corresponding values or use the appropriate PHP functions to get it.
Also ensure that jQuery & your function definition of getServices exists before your PHP condition executes.
I guess your syntax has a problem. You can not invoke Javascript inside a PHP script. This part here:
<?php if (isset($current_date_shipment) && $current_date_shipment == 1) // check box value check{?>
<script type="text/javascript">
document.getElementById('row_ship_date').style.display='none';
document.getElementById('row_ship_time').style.display='none';
getServices();
// <---- notice also here you have no </script> tag :)
<?php }?>
I am just pointing out some minor syntax problem :)
Related
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'];?>">€/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
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()
})
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")
Without submit, you need JavaScript to do that, like this:
<script>
var p = document.getElementByName("price");
</script>
I have a generated list of Buttons:
<?php
echo "<td> <button id=\"{$cell}\" onclick=\"ausfüllen($cell)\">{$cell}</button></td>"
?>
and I want to call a JavaScript function:
<script>
function ausfüllen(text) {
document.getElementById("nv_durchwahl").value = "dw";
}
</script>
to fill out some other <td>:
<td>
<input type="text" id="nv_durchwahl" name="nv_durchwahl" value="">
</td>
This is how i tried it.
The buttons are generated without any problem but it doesn't fill out the form.
Any ideas where I went off in this one?
You forgot to close the function bracket
<script>
function ausfüllen(text) {
document.getElementById("nv_durchwahl").value = "dw";
}
</script>
And there is no element with ID "nv_durchwahl".
try to concatenate this way... the function ausfüllen receives a string, check how is being translated into html code...
<?php
echo "<td> <button id='".{$cell}."' onclick='ausfüllen(".'"'.{$cell}.'"'.")'>".{$cell}."</button></td>";
?>
when you say
fill out the form.
means putting on the element with id "nv_durchwahl" (which should be unique) the value "dw" or the content of "text" variable?
<script>
function ausfüllen(text) {
//set the value of the element with the specific id to "dw"
//text is never used!
document.getElementById("nv_durchwahl").value = "dw";
}
</script>
Hi i have a little problem,my code create a list of buttons and each button have a name(value) from my list (table 'usernames')
....for exemple (table 'usernames' have 3 lines aa-bb-cc my code make 3 button with values aa & bb & cc..............
so what i want to do, is when i click on a button i want to print the value of this button in a div, for exemple if i click on the button who have the value 2 i want to print 2 in the div(i have a problem whit my loop, and i need help please)
this is my code:
$conn=mysqli_connect("localhost", "root", "pwd","sn") or die(mysqli_error());
$que2=mysqli_query($conn,"select usernames from posts", MYSQLI_USE_RESULT); // return a lot of lines on 'usernames'
$re=0;
$datax=array();
$i=0;
while ($r = mysqli_fetch_array($que2))
{
$re = $r["cnt"];
$datax[$i]=$re;
?>
<input id="ppost" name="ppost" value="<?php echo $datax[$i]; ?>" type="submit">
<br>
<script type="text/javascript">
$(function(){
$('#ppost').click(function(){
alert("<?php echo $datax[$i]; ?>");
});
});
</script>
<?php
$i++;
}
Ok, so there're few things to have a look at:
take the <script> out of the loop - one script can take care of all the buttons
you can't give same ID to all your inputs - id="..." has to be unique throughout your script
the JS script (assuming you have jQuery included prior to your ) should be:
<script type="text/javascript">
$(function(){
$('input[type="submit"]').click(function(){
alert($(this).attr('value'));
});
});
</script>
this should do the trick, let me know if this is what you wanted.
I have a dynamic field that get value from database, and I want to check one of the field value if it was greater from the old value or not. If it does greater, i have to give an alert and reset field value back to ''
i want to do it without using document.GetElementById because the field was looped by foreach, I'm tryin to work with document.GetElementByName but got no where. here's my code
<?php foreach ($items->result_array() as $row){ ?>
<tr>
<td><p><?php echo $row['nmbr']; ?> </p>
<p><?php echo $row['urai']; ?><p> </td>
<td><?php echo $row['jml']?> </td>
<td><div class="oldprice"><?php echo $row['oldprice']?></div></td>
<td><input id="pnwrn" class="pnwrn" type="text" name="pnwrn[]" value="" data-harga="<?php echo $row['jml']?>" data-jumlah="<?php echo $row['oldprice']?>"/></td>
<td><div class="output"></div></td>
</tr>
<?php } ?>
and this is my javascript :
<script type="text/javascript">
$('.pnwrn').on('change', function () {
var twrn = $(this).val();
var price = parseInt($(this).data('harga'));
var jmlh = parseInt($(this).data('jumlah'));
if (twrn <= jmlh){
$(this).parents('tr').find('.output').html(twrn*price);
}
else {
alert("Price can not be greater from old price");
cleartext();
}
});
function clearInputs() {
document.getElementByName("pnwrn").value = "";
}
</script>
can someone help me?
You are not supposed to have multiple elements with the same id. Instead, try this:
<td><input id="pnwrn<?php echo $row['some_id_field_that_you_have']; ?>" class="pnwrn" type="text" name="pnwrn[<?php echo $row['some_id_field_that_you_have']; ?>]" value="" data-harga="<?php echo $row['jml']?>" data-jumlah="<?php echo $row['oldprice']?>"/></td>
You can also attach the matching ID on the <tr>, or to other elements, which makes it trivial to find the appropriate elements.
Unfortunately, with names like urai, harga, jumlah and pnwrn, it is extremely hard to pinpoint what exactly you're trying to do, thus just vague hints.
But - if you're trying to clear the very field you're checking, you already have it, so no lookup needs to be done. Just do clearInput(this).
Try this to loop through all the inputs and match on the class you want:
$("input").each(function (i) {
if ($(this).prop("class") === "pnwrn") {
$(this).val('');
}
});
I have a checkbox which stores values from database table. So it contains an array of values when I display them on my page.
<input type="checkbox" id="chkdelt[<?php echo $id; ?>]" name="chkdelt[]" value="<?php echo $id; ?>">
Above given is the checkbox code.
I have a popup block which shows the count of checked checkboxes.
<div class="popup">
Show count here <span id="numb"></span>
<button type="button" onclick="paz(<?php echo json_encode(chkdelt); ?>);">Click</button>
</div>
And I am able to show the count successfully using this javascript function given below.
// used to show number of checked checkboxes in popup
function dumpInArray(){
var arr = [];
$('.table-responsive input[type="checkbox"]:checked').each(function(){
arr.push($(this).val());
});
return arr.length; //returns no of checked check boxes
}
$('.select_chk').click(function () {
$('#numb').html(dumpInArray()); // output of dumpInArray() written into numb span
});
And I try to pass this array to a Javascript function while clicking the button inside of popup.
My Javascript is given below:
function paz(a)
{
var temp = new Array();
temp = JSON.parse(a);
for(i=0;i<temp.length;i++)
{
alert(temp[i]);
}
}
The values are not passed to paz() in Javascript. Rest works fine. I want to pass the values of all checked checkboxes to the paz() when I click the button in the popup div.
Thanks. Help???
You need to pass argument to JavaScript function as a string.
You're passing them as a variable which in turn will be becoming as undefined.
To pass argument as a string use function_name('arg') or function_name("arg").
So in your code change:
<button type="button" onclick="paz(<?php echo json_encode(chkdelt); ?>);">Click</button>
to
<button type="button" onclick="paz('<?php echo json_encode(chkdelt); ?>');">Click</button>
Also I'll suggest you to use <input type="button" /> Instead of <button> & I think if chkdelt is variable name in php you'll need to use $ sign before it and use json_encode($chkdelt).
Hope it'll help you.. Cheers :)!