I have this script:
<?php
header("refresh: 5;");
include 'theme.php';
ceklogin();
css();
echo <<<HTML
<script type="text/javascript">
var textarea = document.getElementById(\"wget\");
textarea.scrollTop = textarea.scrollHeight;
</script>
HTML;
echo " Wget log :<br>";
echo "<textarea name=\"text-info\" rows=\"30\" id=\"wget\" cols=\"90\" readonly style=\"font-family: Arial;font-size: 7pt;\" >";
$datalines = file ("wget.log");
foreach ($datalines as $zz) {
echo $zz; }
echo "</textarea></div>";
foot();
echo '
</div>
</body>
</div>
</html>';
?>
the javascript part doesn't work, it won't scrool to bottom on the textarea every time it refreshes the page, any ideas?
Update: I modified my script to be like this but still doesn't work
<?php
header("refresh: 5;");
include 'theme.php';
ceklogin();
css();
echo '<script type="text/javascript">
var textarea = document.getElementById(\"wget\");
textarea.scrollTop = textarea.scrollHeight;
</script>';
echo " Wget log :<br>";
echo "<textarea name=\"text-info\" rows=\"30\" id=\"wget\" cols=\"90\" readonly style=\"font-family: Arial;font-size: 7pt;\" >";
$file = file_get_contents('/www/wget.log');
echo $file;
echo "</textarea></div>";
foot();
echo '
</div>
</body>
</div>
</html>';
?>
Try:
echo '<script type="text/javascript">
var textarea = document.getElementById(\"wget\");
textarea.scrollTop = textarea.scrollHeight;
</script>';
(Removed the <<<HTML and reformatted the HTML string.)
You could try moving your Javascript to the bottom of the body and putting this in a function:
(function() {
var textarea = document.getElementById(\"wget\");
textarea.scrollTop = textarea.scrollHeight;
})();
Related
So far I have managed that when a button is clicked it will print all values within the array. I need it to be smarter and only print the value of the element clicked.
<?php
$Arr_shoppingList = array(
array("Volvo",22,18,0),
array("BMW",15,13,1),
array("Saab",5,2,2),
array("Land Rover",17,15,3)
);
//Looped through array and printed values
foreach ($Arr_shoppingList as $value) {
echo "<div class='ColumnRow Spacing Color2 Border'>";
echo "<h1> $value[0] </h1> <br>";
echo "<p>'MPG'. $value[1]</p> <br>";
echo "<p>'Stock' . $value[2]</p> <br>";
echo "<form method='GET'>";
echo "<button class='Border' type='submit' id='$value[0]' class='Button'> $value[0]
</button>";
echo "</form>";
echo "</div>";
}
?>
<script>
$('button').on('click', function (event) {
event.preventDefault()
$('button').each(function(index, value){
console.log(value.id)
})
});
</script>
EDIT: The first section has been completed many thanks, but similarly, but I also need the stock and MPG values to also be targeted, how would I go about incorporating that into this code? this is part of a checkout basket I'm attempting to create.
You can use event.target.id to get the id of the clicked element
<?php
$Arr_shoppingList = array(
array("Volvo",22,18,0),
array("BMW",15,13,1),
array("Saab",5,2,2),
array("Land Rover",17,15,3)
);
//Looped through array and printed values
foreach ($Arr_shoppingList as $value) {
echo "<div class='ColumnRow Spacing Color2 Border'>";
echo "<h1> $value[0] </h1> <br>";
echo "<p>'MPG'. $value[1]</p> <br>";
echo "<p>'Stock' . $value[2]</p> <br>";
echo "<form method='GET'>";
echo "<button class='Border' type='submit' id='$value[0]' class='Button'> $value[0]
</button>";
echo "</form>";
echo "</div>";
}
?>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$('button').on('click', function (event) {
event.preventDefault()
console.log(event.target.id);
});
</script>
I focus mostly on PHP. I have a cookie "themeColor"
am trying to make it so a user can select a color and change the background color of 3 divs at will. Currently, I am able to do this with the #menu style
if (isset($_COOKIE['themeColor'])) {
$color = urldecode($_COOKIE['themeColor']);
echo '<script>document.getElementById("menu").style.background="rgb(' . $color . ')"; </script>';
echo '<script>document.getElementById("main").style.background="rgb(' . $color . ')"; </script>';
echo '<script>document.getElementById("footer").style.background="rgb(' . $color . ')"; </script>';
}
the other 2 divs will not change color. I assume it is a syntax error but I have been looking at this for hours now and would appreciate some help
Ask for any data
edit// adding html markup for menu main and footer
"BEGIN FILE header.php" - starting at #menu -
<div id="menu">
<h1>Solor Hive</h1>
</div>
<?php
if (isset($_COOKIE['themeColor'])) {
$color = urldecode($_COOKIE['themeColor']);
echo '<script>document.getElementById("menu").style.background="rgb(' . $color . ')"; </script>';
echo '<script>document.getElementById("main").style.background="rgb(' . $color . ')"; </script>';
}
if (isset($_POST['logout'])) {
setcookie('theme', "", time() - 3600);
setcookie('user', "", time() - 3600);
header("Refresh:0; url=./index.php");
}
?>
<div class="main" id="main">
<div id="content">
"END OF FILE header.php"
"BEGINNING FILE footer.php"
</div>
</div>
<div id="footer" class="footer">
<? if(isset($_COOKIE['user'])) {
$user = $_COOKIE['user'];
echo '<div class="profile">';
echo 'My Profile';
echo 'User Search';
echo 'Chat';
echo 'My Pictures';
echo 'Games';
echo 'Tools';
echo 'Help</div>';
echo '<form method="POST" id="logout" action="#">';
echo '<input type="submit" name="logout" value="logout"/>';
}
?>
<h4>Made by Solorside</h4>
</form>
</div>
</body>
</html>
"colorpicker.php"
<? include("./header.php");
if (isset($_POST['color'])) {
$rgb = $_POST['color'];
setcookie('themeColor', $rgb, time() + (86400 / 12), "/");
}
?>
<canvas width="512" height="512" id="canvas_picker"></canvas>
<form method="POST" id="colorpicker" Action="#">
<div id="rgb">RGB: <input type="text" name="color" id="color"></input>
</div>
<input type="submit" id="submit" value="Change Theme Color">
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script type="text/javascript">
var canvas = document.getElementById('canvas_picker').getContext('2d');
var img = new Image();
img.src = './data/image.png';
$(img).load(function(){
canvas.drawImage(img,0,0);
});
$('#canvas_picker').click(function(event){
var x = event.pageX - this.offsetLeft;
var y = event.pageY - this.offsetTop;
var imgData = canvas.getImageData(x, y, 1, 1).data;
var R = imgData[0];
var G = imgData[1];
var B = imgData[2];
var rgb = R + ', ' + G + ', ' + B;
$('#rgb input').val(rgb);
});
</script>
<? include("./footer.php"); ?>
Move your Javascript/ JQuery just above </body> tag.
I would try without JS, because for me makes no sense to search on the DOM when I have the value stored already on a COOKIE, I would apply on the top of the body the next code:
<style>
<?php if(isset($_COOKIE['themeColor'])) { ?>
.bg-color {
background-color: <?php echo urldecode($_COOKIE['user']); ?>; /* COOKIE */
}
<?php } else { ?>
.bg-color {
background-color: #767577; /* default one */
}
<?php } ?>
</style>
So, I will apply the bg-color class on the desired div I want to change with the cookie, then when the cookie is set it will apply the background color.
i stuck with this one,, i want the the result from js to CI.. i want got string type data from js, and sendback it to CI.
this is my code:
<?php $i=0; foreach ($bahanx as $pin2){ ?> <script type="text/javascript">
document.write("<div class='input-group' style='width:175%'><label class='input-group-addon' style='width:100px'><input type='checkbox' name='bahan' aria-label='Checkbox for following text input' value="+<?php echo $pin2->id_barang ?>+" oninput='changeText(this,"+<?php echo $i ?>+");' > <?php echo $pin2->nama_barang;?></label><input type='text' class='form-control' aria-label='Text input with checkbox' name='banyak_bahan' value='0' disabled/></div><br/>");
</script>
<?php
$i++; } ?>
and this:
var item_box = document.getElementsByName('banyak_bahan');
var item_name = document.getElementsByName('bahan');
var tot;
function changeText(e,i){
item_name[i].value = e.checked ? 1 : 0;
item_box[i].disabled = !e.checked;
var x=x+","+item_box[i];
var y=y+","+item_name[i];
//alert(x);
document.getElementById('fff').innerHTML=x+"<br/>"+y;
}
id=fff
<div id='fff'></div>
the result:
undefined,[object HTMLInputElement]
undefined,[object HTMLInputElement]
Try this you have invalid js string thats because you print +<?php echo $aaa;?>+ inside try to delete +
UPDATE: try this to echo from php directly.
<?php
$i=0;
foreach ($bahanx as $pin2){
echo "<div class='input-group' style='width:175%'><label class='input-group-addon' style='width:100px'><input type='checkbox' name='bahan' aria-label='Checkbox for following text input' value='".$pin2->id_barang."' oninput='changeText(this,".$i.");' > ". $pin2->nama_barang ."</label><input type='text' class='form-control' aria-label='Text input with checkbox' name='banyak_bahan' value='0' disabled/></div><br/>";
$i++;
}
?>
instead of this
<?php $i=0; foreach ($bahanx as $pin2){ ?>
<script type="text/javascript">
document.write("<div class='input-group' style='width:175%'><label class='input-group-addon' style='width:100px'><input type='checkbox' name='bahan' aria-label='Checkbox for following text input' value="<?php echo $pin2->id_barang; ?>" oninput='changeText(this,"<?php echo $i; ?>");' > <?php echo $pin2->nama_barang; ?></label><input type='text' class='form-control' aria-label='Text input with checkbox' name='banyak_bahan' value='0' disabled/></div><br/>");
</script>
<?php $i++; } ?>
UPDATE: you saw x & y is not defined thats number one , to get value of checkbox element you must use item_name[i].value, item_box[i].value
var item_box = document.getElementsByName('banyak_bahan');
var item_name = document.getElementsByName('bahan');
var tot;
function changeText(e,i){
item_name[i].value = e.checked ? 1 : 0;
item_box[i].disabled = !e.checked;
var x= item_box[i].value;
var y= item_name[i].value;
//alert(x);
document.getElementById('fff').innerHTML=x+"<br/>"+y;
}
I am displaying a dynamic table.
The problem is when I am passing the php variable to the javascript function it is showing an error Unterminate string literal.
I am new to php so I dont know how we pass the variable.
<?php
include 'dbConf.php';
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST["submit"]))
{
$username=$_SESSION["username"];
$productName=$_POST["productName"];
$quantity=$_POST["quantityRequired"];
$row = mysqli_query($conn,"INSERT into TR_Order_Details_Dummy values('$username','$productName','$quantity')");
if(!$row) {
die('Could not enter data: ' . mysqli_error($conn));
}
else{
$result=mysqli_query($conn,"SELECT * from Order_Details where Username='".$username."' ");
}
}
else
{
echo "dummy";
}
}
?>
<html>
<head>
<title>Order Details Page</title>
<script type = "text/javascript"
src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<table id="table1">
<th>
<tr></tr>
<tr>Username</tr>
<tr>Product Name</tr>
<tr>Quantity</tr>
</th>
<?php
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['Username']."</td>";
echo "<td>".$res['ProductName']."</td>";
echo "<td>".$res['Quantity']."</td>";
$username=$res['Username'];
echo "<td><input type=\"button\" value=\"Edit\" onClick=\"editItem('\".$username.\"')\"> Delete</td>";
}
?>
</table>
</form>
<script>
function addItem()
{
window.location.href="order.php";
}
function editItem(username)
{
$.ajax({
type:"POST",
url:"edit.php",
});
return false;
}
</script>
</body>
</html>
Following line is incorrect.
echo "<td><input type=\"button\" value=\"Edit\" onClick=\"editItem('\".$username.\"')\"> Delete</td>";
You need to concatenate $username in the string so the quotes around it shouldn't be escaped with \ backslash.
Here's the modified code.
echo "<td><input type=\"button\" value=\"Edit\" onClick=\"editItem('".$username."')\"> Delete</td>";
Try your code with this command line
<input type=\"button\" value=\"Edit\" onClick=\"editItem('".$username."')\">
Im trying to get a value of an input into my buttons action url before the button is pushed.
I need to get the value of:
id="qty<?php echo $i;?>"
Into where it says:
<?php echo VALUE OF $qtyforaction; ?>
My Code is:
<?php $i = $_product->getId();?>
<form action="<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<input type="text" name="qty" id="qty<?php echo $i;?>" maxlength="12" value="1" title="Qty" class="cartnum" />
<?php $qtyforaction = 'qty'.$i; ?>
<?php echo $qtyforaction; ?>
<button type="button" class="addtocartbutton button btn-cart" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>qty/<?php echo VALUE OF $qtyforaction; ?>')" ><span><span>Order Again</span></span></button>
</form>
Would I have to use javascript to get it into the url string? My javascript is a bit ropey, would something like this work?
<script type="text/javascript">
var something= document.getElementById('<?php echo $qtyforaction;?>');
</script>
UPDATE WITH FULL CODE:
<?php
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
/* Get the customer data */
$customer = Mage::getSingleton('customer/session')->getCustomer();
/* Get the customer's email address */
$customer_email = $customer->getEmail();
}
$collection = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_email', array(
'like' => $customer_email
));
$uniuqProductSkus = array();
foreach ($collection as $order) {
$order_id = $order->getId();
$order = Mage::getModel("sales/order")->load($order_id);
$ordered_items = $order->getAllItems();
foreach ($ordered_items as $item)
{
if (in_array($item->getProduct()->getSku(), $uniuqProductSkus)) {
continue;
} else {
array_push($uniuqProductSkus, $item->getProduct()->getSku());
$_product = Mage::getModel('catalog/product')->load($item->getProductId());
$product_small_image_path = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(200);
$product_thumbnail_path = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(150);
$summaryData = Mage::getModel('review/review_summary')->load($item->getProductId());
echo "<li>";
echo "<div class='previous-name'><p><a style='color:black; font-weight:bold; font-size:14px;' href='" . $_product->getProductUrl() . "'>";
echo $item->getName() . "</a></p></div>";
echo "<div class='previous-image'><a href='" . $_product->getProductUrl() . "'>";
echo "<img src='" . $product_small_image_path . "' />";
echo "</a></div>";
echo "<div class='previous-rating'>";
echo "<p><a style='color:black; font-weight:bold; font-size:14px;' href='" . $_product->getProductUrl() . "#product_tabs_review_tabbed'>Review this beer now</a></p>";
echo $summaryData->getRatingSummary() . '% Would buy again <br/>';
echo "<div class='rating-box' style='float:left;'>";
echo "<div class='rating' style='width:" . $summaryData->getRatingSummary() . "%'></div></div>";
echo "</div>";
/**echo "<div class='previous-button'>";
echo '<button type="button" title="Add to Cart" class="button btn-cart" onclick="setLocation(\'';
echo $this->helper('checkout/cart')->getAddUrl($_product);
echo '\')"><span><span>Order Again</span></span></button>';
echo "</div>";**/
?>
<?php $i = $_product->getId();?>
<div class='previous-button'>
<form action="<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<input type="text" name="qty" id="qty<?php echo $i;?>" maxlength="12" value="1" title="Qty" class="cartnum" />
<?php $qtyforaction = 'qty'.$i; ?>
<script type="text/javascript">
var xy = document.getElementsByTagName("input")[0].getAttribute("qty");
var x = "<button type='button' class='addtocartbutton button btn-cart' onclick='setLocation()'><span><span>Order Again</span></span></button>";
document.getElementById("buttonaddcart").innerHTML = x;
</script>
<div id="buttonaddcart">
<button type="button" class="addtocartbutton button btn-cart" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>qty/')" ><span><span>Order Again</span></span></button>
</div>
</form>
</div>
<?php
echo "<div class='previous-clear'></div>";
echo "</li>";
}
}
}
?>
You need to get the value of the id attribute:
var x = document.getElementsByTagName("input")[0].getAttribute("id");
But how do you plan on using PHP to echo out this variable if PHP is loaded and done with before Javascript even comes into play? You could use Javascript though to display this variable on the page if that's all you needed.
If you had something in your HTML to target, you could do it like this:
document.getElementById("result").innerHTML = x;
But anyways, get the action attribute value...
var y = document.getElementsByTagName("form")[0].getAttribute("action");
Then set the action attribute to this + the qty...
document.getElementsByTagName("form")[0].setAttribute("action", x + y);
That should work for you, if not, try wrapping x + y in additional parentheses...("action", (x + y));