I'm trying to add a button under the product name in the cart page for each item which I did, but my alert isn't showing when the button is clicked
function request_button( $product_title, $cart_item, $cart_item_key ) {
$_request = '<div class="request-button"><button type="button" value="save_request" onclick="myFunction()">Request tailormeasurement</button></div>';
return $product_title. '<br/>' . $_request;
if(!$item[$_request]){
?>
<script type="text/javascript">
function myFunction() {
alert("You have requested tailor measurement");
}
</script>
<?php
}
}
add_action('woocommerce_cart_item_name', 'request_button' , 25);
You write return so the function will exit and not continue the rest of code in this function, the if statement that contains tag will not rendered
try use echo or print instead of return here
Related
I want to go back to previous page after pressing "OK" on the function message prompted.
Php Code
function function_alert($message) {
// Display the alert box
echo "<script>alert('$message');
document.location='javascript://history.go(-1)';
</script>";
}
// Function call
function_alert("This email has already subscribed");
}
Alert Box
Thank you so much!
Tried this
function function_alert($message) {
echo "<script>
alert('$message')
history.back()
</script>";
}
I have a page that allow users to access it. This page have certain things that not allow user to see that particular thing, For my case, user is unable to edit edit() or view this function, so I want to hide it by putting below code in JS because I am using function on this link.
I already enabled Session in PHP page like this <?php session_start(); ?>
And I try this putting this PHP in JS inside Datatables function. The dataTable function is working fine. Just the PHP is not working.
{ data : "project_id",
render: function(data) {
return "<span onclick='view()'></span> "+ // This is allow to user and admin level
"<?php
if ($_SESSION['user_privilege'] == 'Admin') { ?>
<span onclick='edit()'</span>
<?php } ?>"; // This is allow for admin level only
}
}
Check the following
<?php
if(isset($_SESSION["user_user_privilege"])){
echo "<script>
let button = '<span onclick='edit()'></span>';
</script>";
}else{
echo "<script>
let button = '';
</script>";
}
?>
Now check the button is empty or not.
{ data : "project_id",
render: function(data) {
return "<span onclick='view()'></span> "+ ((button!="") ? button : "");
}
}
Basically, I have a search box that suggests results from a database as user types. It looks like this :
Example
.
I use ajax in a file to make a live search as user types :
<input name="estab" id="estab" type="text" class="estab" />
<div id="result"></div>
<script>
$("#estab").on("input", function(){
$estab = $("#estab").val();
if ($estab.length > 0){
$('#result').show();
$.get("res.php", {"estab":$estab},function($data){
$("#result").html($data);
})
}
});
<script>
Then in another file (res.php) I have a sql request and I display results. I also have my 'onclick' function to replace suggestion in text box :
while($result=$data->fetch_assoc()){
$sortie[] = $result['school'];
}
$sortie2=array_unique($sortie);
echo '<ul>';
foreach($sortie2 as $value){
echo "<script>
function fill(){
document.getElementById('estab').value = '$value';
$('#result').hide();
}
</script>";
echo "<li onclick='fill()'><b>$value</b></li>";
}
echo '</ul>';
My problem is : when i click on any of the suggestion, it is always the last option that is replaced in the text box, in this case "University of Washington".
I have been trying to solve that problem for 2 days now and I can't find the solution.
Any help would be greatly appreciated. Thanks
UPDATE
Found the solution, if someone is interested, here's what I did in res.php:
while($result=$data->fetch_assoc()){
?>
<li onclick='fill("<?php echo $result['school']; ?>")'><?php echo $result['school'];?></li>
<?php
}
?>
</ul>
and in the first file :
<script>
function fill(val){
$('#estab').val(val);
$('#result').hide();
}
$("#estab").on("input", function(){
$estab = $("#estab").val();
if ($estab.length > 0){
$('#result').show();
$.get("res.php", {"estab":$estab},function($data){
$("#result").html($data);
})
}
});
</script>
echo "<script>
function fill(newValue){
document.getElementById('estab').value = '" + newValue + "'" + ;
" $('#result').hide();
}
</script>";
echo '<ul>';
foreach($sortie2 as $value){
echo "<li onclick='fill($value)'><b>$value</b></li>";
}
echo '</ul>';
I wouldn't use the onclick method here. I would use jQuery to assign the events to your list items. But if you must, in your onclick method, try passing this.value like so:
<li onclick="fill(this.value)"><b>$value</b></li>
Then your fill() method could be something like so:
function fill(val){
$('#estab').val(val);
$('#result').hide();
}
EDIT: You are mixing plain JavaScript and jQuery code. I replaced your plain JS line with the jQuery equivalent.
Also, if you are adding the fill() method in the same loop that adds the LIs, then you are adding multiple fill methods, which is incorrect. Just add it once in the same script tag like below.
<script>
function fill(val){
$('#estab').val(val);
$('#result').hide();
}
$("#estab").on("input", function(){
$estab = $("#estab").val();
if ($estab.length > 0){
$('#result').show();
$.get("res.php", {"estab":$estab},function($data){
$("#result").html($data);
})
}
});
<script>
I am displaying a message box at the client browser. It works fine but i see a blank page at the back when this alert message box pops up. But I want to the user to see his browsing page always and pop up this message box. Here is my code.`
if($_POST['submit']=="Save")
{
$language=$_POST['pbx_lan'];
if($language!="")
{
$query="update `account_detail` set `prompt_language`= '$language' WHERE `org_id`='".$_SESSION['account_id']."'";
$result = $mysqli->query($query) or die($mysqli->error);
$_SESSION['check_update'] = "1";
// setcookie("msg","Language Successfully Updated",time()+5,"/");
echo '<script>
confirm("You have selected '.$language.'");
window.location = "'.SITE_URL.'index.php?view=pbx_prompts."
</script>';
// header("location:".SITE_URL."index.php?view=view_service");
}
else
{
setcookie("err","No Language Selected.Please Select Language",time()+5,"/");
header("location:".SITE_URL."index.php?view=view_service");
}
}`
When an confirm() is called the page stops loading. When you add the confirm() at the end of the page, the page is loaded before the confirm() is shown
<body>
TEST123
<script>
confirm('test');
</script>
</body>
instead of
<body>
<script>
confirm('test');
</script>
TEST123
</body>
For your code this means that
echo '<script>
confirm("You have selected '.$language.'");
window.location = "'.SITE_URL.'index.php?view=pbx_prompts."
</script>';
has to move to the bottom of your body tag
Also copy the required if statements so that the code is only executed when neccesary.
The complete code at the bottom of your body tag should be something like this:
if (!empty($language) && $_POST['submit'] == "Save") {
echo '<script>
confirm("You have selected '.$language.'");
window.location = "'.SITE_URL.'index.php?view=pbx_prompts."
</script>';
}
This is my code. I already tried, flush(); window_location.. but still it fails. What shall I do? I also tested if the value of $result is being passed and it's fine. The only problem is the JavaScript is not loading. I have other codes with this coding structure but works fine and shows the JavaScript before loading.
function accsettings()
{
$this->load->model('admin_model');
$result = $this->admin_model->accsettings();
if ($result==0)
{
echo '
<script type="text/javascript">
alert("Congratulations! Your profile has been updated.");
</script>
';
$res2 = $this->admin_model->accset_audit();
}
else if ($result==1)
{
echo '<script type="text/javascript"> alert("Error! Current password is not correct."); </script>';
}
else
{
echo '<script type="text/javascript"> alert("New password does not match with the confirmation."); </script>';
}
redirect('/main/accsettings');
}
I would not recommend this way to show 'JavaScript' alert message, if you want to show validation message then please set variable controller and check on view.
In you code you are using headerlocation method to redirect page that is not show up result on browser.
Please use refresh method to show buffer result on browser
redirect('/main/accsettings', 'refresh');
//similar to
header( "refresh:0;url=/main/accsettings" );
You should do the redirect also inside the javascript when you want this to be executed.
Here's how you could achieve this:
<script type="text/javascript">
alert("Error! Current password is not correct.");
setTimeout(function () {
window.location.href = "http://www.google.nl"; //will redirect to google.
}, 2000); //will call the function after 2 secs.
</script>