onclick location href not working with php variable - javascript

I have a button with a link that involves a PHP variable, but when the button is clicked, nothing happens and I am not brought to a new page according to the link. I tested the link with a <a> tag instead of a <button> tag, and the link works. I am new to PHP so any suggestions are appreciated.
Here is the code:
<?php
echo
'<button type="button" class="btn btn-secondary" id="btn" style="margin-left:0%;color:gray" onclick="location.href="' . $child_page . '"">
KEEP READING <span class="glyphicon glyphicon-chevron-right" style="color:gray"></span>
</button>';
echo 'Link ';
?>
The variable can look like: /n/2/ar/ and will apply like this: www.example.com/n/2/ar/. Please note that the inline CSS is only for testing purposes.

You have a problem with the quotes
Try this:
<?php
$x = 'http://test.com';
$y = 'location.href="'.$x.'"';
echo
'<button type="button" class="btn btn-secondary"
id="btn"
style="margin-left:0%;color:gray"
onclick='.$y.'>
KEEP READING
<span class="glyphicon glyphicon-chevron-right" style="color:gray"></span>
</button>';
?>

There is a problem with the quotes..Nothing else
Use this:
echo
'<button type="button" class="btn btn-secondary" id="btn" style="margin-left:0%;color:gray" onclick=location.href="'.$child_page.'">
KEEP READING <span class="glyphicon glyphicon-chevron-right" style="color:gray"></span>
</button>';

It looks like you have a problem with your opening tag. The code
$child_page = '/n/2/ar/';
echo '<button ... onclick="location.href="' . $child_page . '"">';
renders to
<button ... onclick="location.href="/n/2/ar/"">
which is understood by browser as
<button ... onclick="location.href=" /n/2/ar/ "">
So the location is being empty.
Try wrapping url into single quotes instead:
$child_page = '/n/2/ar/';
echo '<button ... onclick="location.href=\'' . $child_page . '\'">';

If you begin with double-quotes instead of single-quotes, it can be done this way:
$child_page = 'this.html';
echo "<button type='button' class='btn btn-secondary' id='btn' style='margin-left:0%;color:gray' onclick=\"location.href='$child_page'\">
KEEP READING <span class='glyphicon glyphicon-chevron-right' style='color:gray'></span>
</button>";
echo "<a href='$child_page'>Link </a>";
Double-quotes allow you to place a variable 'as-is' whereas with single-quotes you'll have to do a couple extra steps.

Try this
<?php
$child_page = 'https://www.google.com';
echo '<button type="button" class="btn btn-secondary" id="btn" style="margin-left:0%;color:gray" onclick="location.href=\'' . $child_page . '\'">
KEEP READING <span class="glyphicon glyphicon-chevron-right" style="color:gray"></span>
</button>';
echo "<a href='$child_page'>Link </a>";
?>

Related

jQuery add button attribute using closest method

Here is the button for adding items to cart
<div class="title_6">
<button class="btn my-cart-btn bt_cart btn-large btn-positive"
data-id="<?php echo $row['product_id'];?>"
data-name="<?php echo $row['product_name'];?>"
data-summary="<?php echo $row['pres_name'];?>"
data-price="<?php echo $row['sales_price'];?>"
data-quantity="1"
data-image="admin/upload/<?php echo $row['photo_1'];?>"
>Add to Cart</button>
</div>
I'm trying to set data-quantity attribute of button using jQuery. As there are multiple items I'm using closest method. What I've tried-
$(this).closest('.title_6').find('button').attr("data-quantity", qty);
But its not working. How to do that properly ?
Update:
Here is the code for (+) button. What is tying to do, When (+) button .bt_3 is clicked I'll add attribute to another button "Add Cart"
$(document).ready(function() {
$('.product_row').on('click', '.bt_3', function() {
var qty = $(this).closest('.title_5').find('input').val();
var qty_add = Number(qty)+Number(1);
$(this).closest('.title_5').find('input').val(qty_add);
$(this).closest('.title_6').find('button').attr("data-quantity", qty_add);
});
});
HTML for (+) & (-) button along with Add Cart Button for better understanding
<div class="title_5">
<button class="btn bt_1 btn-large btn-negative">-</button>
<input type="text" class="bt_2" value="1">
<button class="btn bt_3 btn-large btn-primary">+</button>
</div>
<div class="title_6">
<button class="btn my-cart-btn bt_cart btn-large btn-positive" data-id="<?php echo $row['product_id'];?>" data-name="<?php echo $row['product_name'];?>" data-summary="<?php echo $row['pres_name'];?>" data-price="<?php echo $row['sales_price'];?>" data-quantity="1" data-image="admin/upload/<?php echo $row['photo_1'];?>" >Add to Cart</button>
</div>
Try changing this line:
$(this).closest('.title_6').find('button').attr("data-quantity", qty_add);
for this one:
$(this).parent().next('.title_6').find('button').attr("data-quantity", qty_add);

html inside php inside html parsing issue

I'm having this problem
Uncaught SyntaxError: Unexpected end of input
in this line
<?php ... foreach($res as $row) {echo '<input onclick="selectall('.$j.',"flow'.$row['uid'].'","hi'.$row['uid'].'")" type="submit" class="btn btn-primary btn-user btn-block" value="Update" />';} ?>
Output of that line
<input onclick="selectall(5,"flow9C2748C40A24","hi9C2748C40A24")" type="submit" class="btn btn-primary btn-user btn-block" value="Update" />
The problem is here i think
,"flow'.$row['uid'].'","hi'.$row['uid'].'"
because when i remove it, the problem disappear
Appreciate any help !
There are a couple of functions which should possibly help when it comes to formatting strings - namely printf and sprintf (others in this family also exist) - and these allow you to specify placeholders in the string which are substituted with the arguments provided. Using these helps simplify how the strings are escaped
printf(
'<input type="submit" onclick="selectall( \'%1$s\', \'flow%2$s\', \'hi%2$s\' )" value="Update" class="btn btn-primary btn-user btn-block" />',
$j,
$row['uid']
);

Pass ID on onClick Function in Javascript

I am trying to pass the email using the onClick function and I want to show that email in alert:
<td>
<button class="btn btn-primary" name="buton" onclick="myfunction(<?php $email; ?>)" >Edit</button>
</td>
Here is my function:
<script type="text/javascript">
function myfunction(id)
{
alert(id);
}
</script>
You've to make two changes to validate your code :
You should add echo to pass the php variable to JS.
You need to add single quotes since the $email is a string and you've already using double quotes in onclick attribute.
<td>
<button class="btn btn-primary" name="buton" onclick="myfunction('<?php echo $email; ?>')" >Edit</button>
</td>
try this
when you are passing the email(String) it should be in quote and while using php in javascript in this case u have to use echo
<td><button class="btn btn-primary" name="buton" onclick="myfunction('<?php echo $email; ?>')" >Edit</button></td>
What you want is probably the PHP tag <?= that echoes the $email variable as an argument to the JavaScript function. Note that you will need quotation marks for the email:
onclick='myfunction("<?= $email ?>")'
If shorttags are not enabled the long version must be used:
onclick='myfunction("<?php echo $email; ?>")'
echo the variable in the php tag
<td><button class="btn btn-primary" name="buton" onclick="myfunction('<?php echo $email; ?>')" >Edit</button></td>
Your code is working, you are just not echoing the value of $email in a good way:
- Lack of the single quotes in myfunction(' … ')
- Lack of echo inside your php tags myfunction('<?php echo $email ?>')
To avoid that kind of problems…
In your php, I suggest you to use heredoc syntax:
echo <<< EOD
<td><button class="btn btn-primary" name="buton" onclick="myfunction('{$email}')">Edit</button></td>
EOD;
Note the { } around the variable. They are not necessary, but I suggest you to use them, as they make the variables more noticable.
⋅ ⋅ ⋅
The result will be:
function myfunction(id) {
alert(id);
}
<td><button class="btn btn-primary" name="buton" onclick="myfunction('myemail#stackoverflow.com')">Edit</button></td>
Documentation about Heredoc syntax: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
I hope it helps.
function myfunction(id)
{
alert(id);
}
<td>
<button class="btn btn-primary" name="buton" onClick="myfunction('<?php echo $email; ?>')" >Edit</button></td>
You need to put the email in 'single quotes'

How to add phone numbers from database to <a href="tel:"> tag

<button type="button" class="btn" id="CallBtn"> CALL</button>
I need to add phone numbers from database to the anchor tag above.
How do I do that?
Edit from Comment:
$sql_m="select * FROM b2b_mec_tbl where b2b_shop_id=$b2b_shop_id"; // tracking table $sql_mec=mysqli_query($con,$sql_m) or die(mysqli_error());
$row_mec=mysqli_fetch_array($sql_mec);
$b2b_mec_num=$row_mec['b2b_mobile_number_1'];
You were close here is how I would do it.
<?php
$con='DBCONNECT STRING HERE';
$sql_m="select * FROM b2b where b2b_id = '$b2b_id'";
$sql_mec=mysqli_query($con,$sql_m) or die(mysqli_error());
while($row_mec=mysqli_fetch_array($sql_mec)){
$b2b_mec_num=$row_mec['b2b_mobile'];
}
?>
<a href="tel:<? echo $b2b_mec_num; ?>">
<button type="button" class="btn" id="CallBtn">
CALL
</button>
</a>

jquery show/hide with php for label/input text

Using php/jquery I'm listing out rows data from mysql db using php while loop
When I click on edit button on particular record the label is replaced with input text!! and label value is passed to input text but it showing up only in first row record I'm unable to replace particular row record from label to input.
I'm using 4 links button for edit/delete/save/cancel
when i click on edit button edit/delete button will change to save/cancel and vice versa.
Any help is appricated thanks!!
jquery code
$('body').delegate('.edit', 'click', function() {
$('#feed_label').hide();
$('#url1').show();
});
$('body').delegate('.cancel', 'click', function() {
$('#feed_label').show();
$('#url1').hide();
});
html/php code
<div>
<label style="display:block-inline;" class="feed_label" id="feed_label" idl='<?php echo $row->id;?>'>
<?php echo $row->url; ?>
</label>
<input name="url1" class="form-control url1" value="<?php echo $row->id;?>" id="url1" type="text" placeholder="http://feeds.com/" style="display:none;">
</div>
<div>
<a ide='<?php echo $row->id;?>' id="edit" class='edit' href="#" style="display:block-inline;">EDIT</a>
<a idu='<?php echo $row->id;?>' id="update" class='update btn btn-primary btn-sm' href='#' style='display:none;'>SAVE</a>
<a idd='<?php echo $row->id;?>' id="delete" class='delete' href="#" style="display:block-inline;">DELETE</a>
<a idc='<?php echo $row->id;?>' id="cancel" class='cancel btn btn-warning btn-sm' href='#' style='display:none;'>CANCEL</a>
</div>

Categories

Resources