How to pass two PHP variables in HTML button onClick function? - php

I need to pass these two variables in my HTML button.
$value['_id']
$sellerInfo[0]['City']
The HTML is:
<button id='myBtn' onclick='showproduct()'>Pending</button>
I have tried with this code:
<button id='myBtn' onclick='showproduct("<?php echo $value['_id'];?> , <?php echo $sellerInfo[0]['City']; ?> ")'>Pending</button>
I'm getting an error the second parameter is undefined.
How can I pass the two variables in showproduct()?

The quotes are messed.
Change your code to this:
<button id='myBtn' onclick='showproduct("<?php echo $value['_id'];?>" , "<?php echo $sellerInfo[0]['City']; ?>")'>Pending</button>

Try this
<button id='myBtn' onclick='showproduct("<?php echo $value['_id'];?>" , "<?php echo $sellerInfo[0]['City']; ?>")'>Pending</button>

You can try these tricks in your onClick function from this example
<i class="fa fa-times pl-5 in_time_late_approve" onclick='in_time_late_approve("<?php echo $key ?>", "<?php echo $employee_attendance_item->attendance_in_time ?>")' style="color:green; cursor:pointer" aria-hidden="true"></i>

Related

Need help passing two php variables through a url to the next page

I'm new to Php but what I need this php code to do is to pass two variables through the URL like this
<td align="center"><a class="btn btn-danger" href="reserve.php?distroid=<?php echo $row["distroid"] echo $row['orderid']; ?>">Place Order</a></td>
When we try and do it with just distroid, it works completely fine. But I'm unsure how to pass both values.
Any help would be appreciated.
EDIT: SOLVED IT THANK YOU FOR THE HELP ALL
Try to add more key for the query string like this :
<td align="center">
<a class="btn btn-danger"
href="reserve.php?distroid=<?php echo $row["distroid"]; ?>&orderid=<?php echo $row['orderid']; ?>">
Place Order
</a>
</td>
Then you can get both distroid and orderid
Try this query string like this :
<td align="center">
<a class="btn btn-danger"
href="reserve.php?distroid=<?php echo $row["distroid"]."&orderid=".$row['orderid']; ?>">
Place Order
</a>
</td>
Then you can get both variable distroid and orderid
Hi you can try this anchor tag, it will work for you:
<a class="btn btn-danger" href="reserve.php?distroid=<?php echo $row['distroid'];
?>&orderid=<?php echo $row['orderid']; ?>">
Place Order
</a>

Onclick events inside php code to change an image outside of the code

Prior to switching our site to PHP/MySQL, we had a click event that would change an image when a different button was selected so users could click in between different items and see the differences. I had it set up as an image id originally.
<img id="Demo" src="<?php echo $default; ?>">
This still worked until I added a visibility toggle on the buttons (as we have some items with more options than others.
My php code for each button looks similar to this:
<div class="tooltip">
<span class="tooltiptext"><?php echo $Color3; ?></span>
<?php
if($Hex3==NULL) {
echo '<button class ="button button3" style="display:none"></button>';
} else {
echo'<button type="button" class="button button3" onClick='document.getElementByID("Demo").src = echo $image3;'></button>';
?>
You're using the wrong quotes in your onClick attribute and breaking out of the quoted PHP string.
Is that the only problem? You do not really describe the issue.
echo'<button type="button" class="button button3" onClick='document.getElementByID("Demo").src = echo $image3;'></button>';
should be...
echo "<button type='button' class='button button3' onClick='document.getElementById(\"Demo\").src = \"{$image3}\"'></button>";
Here seems to be a problem
echo "<button type='button' class='button button3' onClick=\" document.getElementById('Demo').src = '{$image}'\"></button>";
Or
echo "<button type='button' class='button button3' onClick=\" document.getElementById('Demo').setAttribute('src', '{$image}') \"></button>";

handling with single quotes in php codeigniter

I want to use html syntax in php and i want to use . instead of <?php ?> my code ig given below ,
$delete='<a class="confirm" onclick="return delete_event(<?php echo $value->id; ?>, '<?php echo base_url() . 'webtv/delete_channels' ?>', '<?php echo current_full_url(); ?>');" href="" ><i class="glyphicon glyphicon-trash text-red del" title="Delete"></i></a>';
Please help me.
<?php
$value = "value_1"; //$value->id;
$baseurl = "base_url"."/webtv/delete_channels"; //base_url()."/webtv/delete_channels";
$fullurl = "current_url"; //current_full_url();
$delete="<a href='#' class='confirm' onclick=\"return delete_event('".$value."','".$baseurl."','".$fullurl."')\" >Click me</a>";
echo $delete;
?>
<script>
function delete_event(var1,var2,var3){
alert(var1+" -- "+var2+" -- "+var3);
}
</script>
You can use sprintf function to solve this issue.
Solution is:
$delete = sprintf('<a class="confirm" onclick="return delete_event(%d, \'%s\',\'%s\');" href="" ><i class="glyphicon glyphicon-trash text-red del" title="Delete"></i>jkhklh</a>', $value->id, base_url() . 'webtv/delete_channels', current_full_url());
I cleaned up your php syntax. I don't quite understand what you're trying to do, but the declaration below should give you an a tag containing what you want (a confirm delete button):
$delete='<a class="confirm" onclick="return delete_event($value->id, '.base_url().'webtv/delete_channels, '.current_full_url().');" href="" ><i class="glyphicon glyphicon-trash text-red del" title="Delete"></i></a>';
If you echo this on your webpage, assuming all of your other links are correct, it should work.

how to use a variable value in anchor's href

<a href="uploads/templates'.$row[0].'/index.php?id=<?php echo $row[0];?>"
class="btn btn-warning">
View
</a>
above is the button and in this button i want to give a path in which folder named templates should be like templates1 ,templates2, templates3. So the numeric value is a variable and 0to be attached with templates so that the folder can open and the index file can be run..so can any one help me with this
<a href="uploads/templates/<?php echo $row[0]; ?>/index.php?id=<?php echo
$row[0];?>" class="btn btn-warning">
View</a>
OR
<?php
echo '<a href="uploads/templates/'.$row[0].'/index.php?id='.$row[0].'"
class="btn btn-warning">
View</a>';
?>
<a href="uploads/templates<?php echo $row[0];?>/index.php?id=<?php echo $row[0];?>" class="btn btn-warning">
View
</a>
If you are writing PHP code inside HTML, you need to wrap your code in <?php ?>.
Your code should be:
<a href="uploads/templates<?php echo $row[0]; ?>/index.php?id=<?php echo $row[0];?>" class="btn btn-warning">
View
</a>

dynamic creation of html elements using php and doing pagination using javascript

I am creating a element dynamically in html and want to use it in a javascript function by referring to its ID. But as its ID is dynamically generated using PHP , so how do i pass it to the javascript function into getElementById?
Actually , i am trying to do pagination using javascript. I have displayed Page No.s, and put HREF on them, but onclick function am stuck up? Please help. My code is as below:
<?php
echo 'Page';
while($d>0)
{
?>
<input type="hidden" value="<?php echo $z; ?>" name="pagination" id="<?php echo 'pagination' .$z ; ">
<label id="<?php echo 'labelofpagination'.$z; ?>" >
<a href="#" onclick="PaginationLabelClicked(); submit();" >
<?php echo $z; ?>
</a>
</label>
<?php
$z++;
}
?>
in your javascript functio you can use the same PHP expression to get the id
var id="<?php echo '\"' .$z '\"'; >";
or you can have it like this,
<script type="text/javascript" language="javascript">
<!--
<?php
echo("id= $z;");
?>
// -->
</script>
to get the id
<label id="<?php echo 'labelofpagination'.$z; ?>" >
<a href="#" onclick="PaginationLabelClicked('<?php echo "\"" ."labelofpagination".$z ."\""; ?>'); submit();" >
<?php echo $z; ?>
</a>
</label>
and javascript
function PaginationLabelClicked(id){
// you can get the id here...
}

Categories