JavaScript conditional ignoring IF - javascript

I have a conditional statement set on the url,I tested both the IF and ELSE and it does work with a simple document.write. However, I am trying to output Php i the conditional but it just always outputs the else statement, Php is not my strongest language:
<script type="text/javascript">
if(
window.location.href=="mywebsite.com"
)
{
document.write('<label for="qty"><?php echo $this->__("Qty:") ?></label><input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" />')
}else{
document.write('<label for="qty"><?php echo $this->__(' ') ?></label>
<input type="hidden" name="qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" />')
}
</script>

window.location.href contains the full URL. If you just want to check the hostname part of the URL, use window.location.host.
if (window.location.host == "mywebsite.com") {
...
}
Another option would be to do all the work on the server.
<?php
if ($_SERVER['SERVER_NAME'] == 'mywebsite.com') {
?>
<label for="qty"><?php echo $this->__("Qty:") ?></label>
<input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" />
<?php } else {
?>
<label for="qty"><?php echo $this->__(' ') ?></label>
<input type="hidden" name="qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" />
<?php }

window.location.href returns the full URL including protocol (so http://www.mywebsite.com).
You could alert the value of window.location.href to see exactly what it's returning, and update your if statement to match it.

Related

How to get a input value from dynamic list

I don't know how to get the value, with js always show me the first input value! Thanks in advance!!!
<html>
<head>
</head>
<body>
<?php
while($i < $forid){
?>
<div id="mydiv">
<input type="text" id="myid" name="myid" value="<?php echo $arrayid[$i]; ?>">
<input type="text" id="myname" name="myname" value="<?php echo $arrayname[$i];?>">
<input type="text" id="myjob"name="myjob" value="<?php echo $arrayjob[$i]; ?>">
<input type="submit" value="get values" id="getvaluesbutton">
</div>
<?php
$i++;
}
?>
</body>
</html>
Get array access to elements by class/name in your js script by changing html:
<?php
while($i < $forid){
?>
<div id="mydiv">
<input type="text" name="myid[]" class="myid" value="<?php echo $arrayid[$i]; ?>">
<input type="text" name="myname[]" class="myname" value="<?php echo $arrayname[$i];?>">
<input type="text" name="myjob[]" class="myjob" value="<?php echo $arrayjob[$i]; ?>">
<input type="submit" value="get values" id="getvaluesbutton">
</div>
<?php
$i++;
}
?>
for javascript hints read this question

Validate all the inputs of a table in jquery

I have a set of inputs that are dynamically generated and their id's incremented based on the number of items in an array
<table width="100%">
<?php $i = 1; ?>
<?php foreach ($products as $product) { ?>
<tr>
<td>
<p><b><?php echo $product['name']; ?></b></p>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<div class="form-group">
<label class="control-label" for="firstname_<?php echo $i; ?>">First Name:</label>
<input name="firstname_<?php echo $i; ?>" id="firstname_<?php echo $i; ?>" required>
</div>
</td>
<td>
<div class="form-group">
<label class="control-label" for="lastname_<?php echo $i; ?>">Last Name:</label>
<input name="lastname_<?php echo $i; ?>" id="lastname_<?php echo $i; ?>" required>
</div>
</td>
<td>
<div class="form-group">
<label class="control-label" for="recipient_number_<?php echo $i; ?>"> Phone Number:</label>
<input name="recipient_number_<?php echo $i; ?>" id="recipient_number_<?php echo $i; ?>" required>
</div>
</td>
<td>
<div class="form-group">
<label class="control-label" for="recipient_email_<?php echo $i; ?>"> Email:</label>
<input name="recipient_number_<?php echo $i; ?>" id="recipient_email_<?php echo $i; ?>" required>
</div>
</td>
</tr>
<?php $i++; ?>
<?php } ?>
</table>
I am trying to look for a way using JQuery to loop through all the generated inputs to ensure they are not empty
Try this snippet.
This will allow you to validate the fields which are empty, and try to use the filter, that helps you to do it without any loop.
More information filter
$('#sender_container').on('submit',function(e)
{
e.preventDefault();
validate();
});
function validate()
{
$('#sender_container > input[type="text"]')
.removeClass('error')
.filter(function() {
// Remove error classes. Filter
return !$.trim(this.value);
})
.addClass('error');
}
.error {
border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" id="sender_container">
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="submit" name="submit" />
</form>
try using this -
you can replace table with a table Id here - $('#tableId :input')
$('table :input').each(function(){
//Enter your code to validate input
console.log($(this).attr('name') + " : " + $(this).val());
});

How to use dynamically if statement php

I have multiple enquiry forms and have different fields. I want to check validation for each field I have in different forms with ajax
Below is my example code:
if($name && $email && $mobile && $departuredate && $vehicle && $query){
?>
<form action="<?php echo DOMAIN; ?>contact/booking-form.php" method="post">
<input type="hidden" name="name" value="<?php echo $name; ?>" />
<input type="hidden" name="email" value="<?php echo $email; ?>" />
<input type="hidden" name="mobile" value="<?php echo $mobile; ?>" />
<?php if(isset($_POST['vehicle'])) {?>
<input type="hidden" name="vehicle" value="<?php echo $vehicle; ?>" />
<?php } ?>
<input type="hidden" name="departuredate" value="<?php echo $departuredate; ?>" />
<input type="hidden" name="query" value="<?php echo $query; ?>" />
<input type="hidden" name="enquiry" value="<?php echo $enquiry; ?>" />
<input type="hidden" name="page" value="<?php echo $page; ?>" />
<input type="hidden" name="agree" value="<?php echo $agree; ?>" />
<input type="hidden" name="submit" id="submit">
</form>
<script>
var form = document.getElementById('submit').form;
var submit_method = document.createElement("form").submit;
submit_method.call(form);
</script>
<?php } ?>
In given code $vehicle is not fix (sometime this value is blank). How can I check if form has vehicle field then check empty and proceed
You can use this same if statement in your javascript
<?php if(isset($_POST['vehicle'])) {?>
//if the field exists write your js validation otherwise . . .
<?php } ?>
List all the fields
Gather all the required fields
Validate
If all validated, process the data
booking-form.php
<?php
// Fields
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$departuredate = $_POST['departuredate'];
$vehicle = $_POST['vehicle'];
$query = $_POST['query'];
// other fields
// A set of required fields
$required_fields = array(
$name, $email, $mobile, $departuredate, $vehicle, $query
);
$message = [];
$is_ok = 1;
// Validate
foreach ($required_fields as $each) {
if (empty($each)) {
$message[] = 'Fields with (*) are required';
$is_ok = 0;
}
}
// If all validated
if ($is_ok) {
// process the data
}
?>
<ul class="errors">
<?php
// Display errors in list
foreach ($message as $each_msg) {
echo "<li>$each_msg</li>";
}
?>
</ul>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" name="name" value="<?php echo $name; ?>" />
<input type="hidden" name="email" value="<?php echo $email; ?>" />
<input type="hidden" name="mobile" value="<?php echo $mobile; ?>" />
<?php if (isset($_POST['vehicle'])) { ?>
<input type="hidden" name="vehicle" value="<?php echo $vehicle; ?>" />
<?php } ?>
<input type="hidden" name="departuredate" value="<?php echo $departuredate; ?>" />
<input type="hidden" name="query" value="<?php echo $query; ?>" />
<input type="hidden" name="enquiry" value="<?php echo $enquiry; ?>" />
<input type="hidden" name="page" value="<?php echo $page; ?>" />
<input type="hidden" name="agree" value="<?php echo $agree; ?>" />
<input type="hidden" name="submit" id="submit">
</form>
<script>
var form = document.getElementById('submit').form;
var submit_method = document.createElement("form").submit;
submit_method.call(form);
</script>
Hi you can use like this.
<?php
function function1() {
echo "this is function 1";
}
function function2() {
echo "this is function 2";
}
if ($page)
eval("{$page} ();");
echo "<a href='$PHP_SELF?page=function1'>Function 1</a>";
echo "<a href='$PHP_SELF?page=function2'>Function 2</a>";
?>

Using window.onload to submit a form

I am trying to submit a form when the page loads. I have tried quite a few different things and none of them seem to work. If I use
window.onload = function(){
alert("test");
}
It works fine but when I try to use
window.onload = function(){
console.log($("#testForm")[0].submit)​, 2000);
}
or
window.onload = function(){
document.getElementById("testForm").submit();
}
It no longer works.
JS:
window.onload = function(){
document.getElementById("testForm").submit();
}
HTML:
<form name="testForm" id="testForm" method="POST" action="test.php">
<input name="MERGE1" type="text" id="fname" size="40" maxlength="40" value="<?php echo $fname; ?>" />
<input name="MERGE2" type="text" id="lname" size="40" maxlength="40" value="<?php echo $lname; ?>" />
<input name="MERGE0" type="text" id="email" size="40" maxlength="40" value="<?php echo $email; ?>" />
<input name="MERGE3" type="text" id="company" size="40" maxlength="40" value="<?php echo $company; ?>" />
<input name="MERGE6" type="text" id="phone" size="40" maxlength="40" value="<?php echo $phone; ?>" />
<input name="MERGE19" type="text" id="city" size="40" maxlength="40" value="<?php echo $city; ?>" />
<input name="Continue" type="submit" value="Continue" />
</form>
Try this:
$("#testForm").submit()
I replaced the POST with a GET and the action with a working URL so you can see something happening.
Working fiddle

javascript to check checkbox when input box is ticked - checkbox array []

I have a simple javascript issue. I have a checkbox and a input box.
when the user inputs text into an input box (onkeydown) I want it to check the corresponding checkbox in the row.
My checkbox's all share the same name for an array checkbox[].
my simplified syntax is:
<table>
<?php if(isset($records)) : foreach ($records as $row) : ?>
<tr>
<td>
<input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>">
</td>
<td>
<input type="text" name="customer_name_<?php echo $row->id ?>" id="customer_name_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" onclick="document.getElementById('editcustomer<?php echo $row->id ?>').checked = true;">
</td>
<tr>
<?php endforeach ; ?>
</table>
As the name of the checkbox is not unique, only the value thereof how do I tell the javascript which checkbox to check?
<input type="text" name="customer_name_<?php echo $row->id ?>" id="customer_name_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" onclick="document.getElementById('editcustomer<?php echo $row->id ?>').checked = true;">
Help appreciated as always.
Thanks
HTML
<input type="checkbox" name="checkbox[]"/>
<input type="text" name="name1" id="name1" />
<br>
<input type="checkbox" name="checkbox[]"/>
<input type="text" name="name2" id="name2" />
<br>
<input type="checkbox" name="checkbox[]" />
<input type="text" name="name3" id="name3" />
Jquery
$(':text').change(function(){
$(this).prev(':checkbox').attr('checked','true');
});
Hope this helps.
Regards.

Categories

Resources