javascript not reading PHP variable correctly in window.open - javascript

I have a very simple table where I'm retrieving income information for some people.
The table retrieves data correctly and the link to the file in stored in a variable, like this:
$file = "/".$row[9];
Where $row comes from the while loop. I have a button inside the table:
<button id = "openbtn" class = 'actionbtn' onClick = "window.open('<?php echo $file;?>')" title = 'Open Attachment'><i class='fa fa-file'></i></button>
If I create a new variable where I manually set the link:
$file2 = '/folder1/folder2/somename.pdf';
then the button will open fine. But it will not work with $file , I get Uncaught SyntaxError: Invalid or unexpected token.
Any clue?

foreach($row as $myfile){
echo '<button
id="openbtn"
class="actionbtn"
title="Open Attachment"
onclick="window.open('."'$myfile'".');"
>
<i class='fa fa-file'></i>
</button>';
}
try this loop

A combination for trim($file) and changing the "" did it:
<button id = "openbtn" class = 'actionbtn' onClick = 'window.open(<?php echo '"'.$file .'"';?>);' title = 'Open Attachment'><i class='fa fa-file'></i></button>

Related

Getting data from a current HTML, PHP form using JavaScript

I have a HTML/PHP sale form which I take in all the details of a sale(products, price etc.).
When I "hold" the sale I then pass the id of the sale, using PHP like the following:
<button type="submit" class="btn btn-primary pull-right btnPrintOrder" saleCode="<?php echo $code; ?>" value="hold" name="openTable">Hold</button>
The desired end result is it is supposed to set off a function to print out the sale details to seperate printers(bar/restaurant).
The print function works as I have tested it against older sales already saved to the database.
My problem is it is not registering the details of the current sale and the receipts are blank.
I have tried to put a delay on it to see could it pick the details up after they have been saved to the database but I'm not having any luck.
The function the button passes to is:
$(".saleForm").on("click", ".btnPrintOrder", function(){
var saleCode = $(this).attr("saleCode");
// setTimeout(food(saleCode), 50000);
// setTimeout(drink(saleCode), 50000);
window.open('extensions/tcpdf/pdf/food_order.php?code='+saleCode);
window.open('extensions/tcpdf/pdf/drink_order.php?code='+saleCode);
})
And the PHP code calling the details in drink_order.php is:
public function getDrinkReceiptPrinting(){
// Sale Info
$itemSale = "code";
$saleValue = $this->code;
$saleAnswer = OpenTableController::ShowTableController($itemSale, $saleValue);
$saledate = substr($saleAnswer["date"],0,-8);
$products = json_decode($saleAnswer["products"], true);
$findCategory = 2;
$catProducts = array_filter($products, function ($product) use ($findCategory) {
return $product['category'] == $findCategory;
});
$netPrice = number_format($saleAnswer["netPrice"],2);
//User Info
$itemUser = "id";
$userValue = $saleAnswer["idSeller"];
$userAnswer = UserController::ShowUsersController($itemUser, $userValue);
Any ideas would be great.
Change the PHP button attribute to this:
<button type="submit" class="btn btn-primary pull-right btnPrintOrder" data-sale-code="<?php echo $code; ?>" value="hold" name="openTable">Hold</button>
use data attribute (dataset attribute), since saleData is not standard attribute, and inside jQuery use .data() to get the value (note how to get it in jQuery):
$(".saleForm").on("click", ".btnPrintOrder", function(){
var saleCode = $(this).data("sale-code");
// setTimeout(food(saleCode), 50000);
// setTimeout(drink(saleCode), 50000);
window.open('extensions/tcpdf/pdf/food_order.php?code='+saleCode);
window.open('extensions/tcpdf/pdf/drink_order.php?code='+saleCode);
})

Getting HREF Values from content on page

I am getting data from a page that is formatted like this
<span id="RANDOMINFO">
+
<span title="1">DATA I WANT HERE</span>
CLICK
MORE RANDOM DATA
</span>
<span id="RANDOMINFO">
+
<span title="2">DATA I WANT HERE</span>
CLICK
MORE RANDOM DATA
</span>
How can I get the href value from the page
Here is the code I have to get the data from the span ID but don't know how to do it for the href as there is no name or id
$doc = new DOMDocument();
#$doc->loadHTML($html2);
foreach($doc->getElementsByTagName('span') as $element )
{
if (!empty($element->attributes->getNamedItem('id')->value))
{
$filename = 'newpks/'.$f.'.txt';
$file = fopen($filename,"a");
$data = $element->attributes->getNamedItem('id')->value.PHP_EOL;
fwrite($file,$data);
fclose($file);
$i++;
$end = $start;
}
}
I assume you're only interested in links with the href attribute, and then we know the tags will be of type a. This should sufficient (I haven't been able to test the code though).
I optimized the code a bit, since the DOMNode class inherits from DOMElement you can use the hasAttribute and getAttribute instead.
foreach($doc->getElementsByTagName('a') as $element ) {
if ($element->hasAttribute('href')) {
$href = $element->getAttribute('href');
// Do your work here
}
}

Javascript that can on multiple buttons dynamically generated from PHP

I have a javascript that creates a modal popup with jquery-confirm. It works on a single instance, but when there are multiple instances with the same onClick events classname they all trigger at once. The idea is a list of videos to delete with a confirmation button.
How can I have one script address dynamic list with unique class names? [i.e] class="video1" class="video2" or using ID or something else so that the confirmation code only triggers uniquely?
PHP and HTML (for example this could generate ten of the HTML line below):
$stuff = mysql_query(
"SELECT * FROM videolist where user_id = '$id' ORDER BY creation_date DESC"
);
while ($ratch = mysql_fetch_array($stuff)) {
$video_id= $ratch["id"];
$title= $ratch["title"];
?>
<a class="delete" href="delete.php?d=<? echo $video_id; ?>">
DELETE VIDEO!
</a>
<? } ?>
JAVASCRIPT:
$('a.delete').confirm({
buttons: {
confirm: function () {location.href = this.$target.attr('href');},
close: function () {}
}
});
First: I would put an id-attribute in the a-tags
<a class="delete" id="v1"></a>
<a class="delete" id="v2"></a>
<a class="delete" id="v3"></a>
Second: I would make an on click this id event
$('.delete').click({
alert($(this).attr("id"));
});
Third: send the id to the delete-script

window.open doesn't do anything

I'm trying to show some info in a pop-up window usig a window.open.. but when I click nothing happens.
This is the function that opens a new window.
function goNewWin(sell_code){
// Set height and width
var NewWinHeight=1000;
var NewWinWidth=1000;
// Place the window
var NewWinPutX=10;
var NewWinPutY=10;
//Get what is below onto one line
TheNewWin =window.open("renovations_history.php?sell_code=" + sell_code,
'Renovaciones','fullscreen=yes,toolbar=no,location=no,status=no,menubar=no,scrollbars=no');
//Get what is above onto one line
TheNewWin.resizeTo(NewWinHeight,NewWinWidth);
TheNewWin.moveTo(NewWinPutX,NewWinPutY);
}
</script>
Here's the HTML, with some PHP.
<form>
<input type="button" value="Renovaciones" onClick="goNewWin(<?php echo $row['sell_code']; ?>)">
</form>
Nor it works with:
Renovaciones
It's 2:22 am and I can think straight, please help.
You need to add single quotes to the JavaScript function call:
<?php
$sell_code = $row['sell_code'];
?>
<input type="button" value="Renovaciones" onClick="goNewWin('<?php echo $sell_code]; ?>')">
Without it, Javascript will consider your $row['sell_code'] as a javascript keyword.
You need to pass it as a string (function parameter).
Reference
if $row['sell_code'] is a string the you can use
$sellCode = $row['sell_code'];
onClick="goNewWin('<?php echo $sellCode; ?>')">

javascript function with 2 arguments is not working

DEscription :
I have a php script that displays the div on an html page
echo '<div class = "star_box" id = "'.$each_status['post_id'].'" onmousedown = "star_it(this.id)"></div>';
Now when I click on this div the star_it function gets called and it works perfect...
The problem
I want to pass another argument in the star it function .. like this
echo '<div class = "star_box" id = "'.$each_status['post_id'].'" onmousedown = "star_it(this.id,'.$each_status['regno'].')"></div>';
or a simple alphabet if any like star_it(this.id,s)
but when I do so the function stops working as when I click the div the function does not gets called ....
I dont know why I am stuck and now my star rating system does not work... I have absolutely no idea what is wrong
Anyone ??
You should modify your string quote's like this :
<?php
$status = "second test";
echo '<div class="star_box" id="'.$id.'" onmousedown="star_it(this.id,'."'".$status."'".');" >Hello</div>';
?>
<script>
function star_it(foo1,foo2){
console.log(foo1);
console.log(foo2);
}
</script>
This example works for me.
So with your code :
<?php
echo '<div class="star_box" id="'.$id.'" onmousedown="star_it(this.id,'."'".$each_status['regno']."'".');" >Hello</div>';
?>
Here you go
echo '<div class = "star_box" id = "'.$each_status['post_id'].'" onmousedown = "star_it(this.id,\''.$each_status['regno'].'\')"></div>';
you forget to add \' before and after your string , the JS engine on the browser will treat your string as an undefined variable and the function will not work because of that .
//my English is not so good.
Try
echo '<div class = "star_box" id = "'.$each_status['post_id'].'" onmousedown = "star_it(this.id,\"'.$each_status['regno'].'\")"></div>';

Categories

Resources