How do I use Javascript in PHP? - javascript

I have got PHP code to show a JavaScript button to the logged in users and it wont display anything if they are not logged in. But it only gives me a white page? The code itself is working because I've used it before but not with javascript.
<div align="center">
<?php
session_start();
if (!isset($_SESSION['user']))
{
echo "";
}else{
echo"<div align="right">
<input type="button" onclick="document.title = 'Google';" value="Change Title to Google">";
}
?>

YOUR CODE HAving ERROS with quotes, added \,
<?php
session_start();
if (!isset($_SESSION['user']))
{
echo "";
}else{
echo "<div align=\"right\">
<input type=\"button\" onclick=\"document.title = 'Google';\" value=\"Change Title to Google\">";
}
?>

Another solution would be to close your php when you don't need it :
<?php
session_start();
if (!isset($_SESSION['user']))
{
?>
<?php
}else{
?>
<div align="right"> ....
<?php
}
?>

Related

Displaying images using for loop on PHP

I am trying to display my images in a for loop in the form tag using PHP but it does not seem to work. I have also tried doing a return statement but nothing still appears.
<?php
$data = array("images/architecture-57e8d34a48_640.jpg",
"images/gateway-arch-57e2d64548_640.jpg",
"images/horseshoe-bend-57e6d6434f_640.jpg",
"images/lake-irene-57e6d24a4d_640.jpg",
"images/silhouette-57e8d5444e_640.jpg");
function displayCheckboxes(){
for ($i=0; $i<count($data); $i++) {
echo "<img src='".$data[$i] . "<br>";
}
}
?>
<body>
<main id ="main">
<form id="pics" action="process.php" method="get">
<label>Name: </label>
<?php echo displayCheckboxes();?>
</form>
</main>
</body>
Maybe Like This:
<?php
$data = array("images/architecture-57e8d34a48_640.jpg",
"images/gateway-arch-57e2d64548_640.jpg",
"images/horseshoe-bend-57e6d6434f_640.jpg",
"images/lake-irene-57e6d24a4d_640.jpg",
"images/silhouette-57e8d5444e_640.jpg");
function displayCheckboxes(){
global $data;
for ($i=0; $i<count($data); $i++) {
echo "<img src='".$data[$i]."'>" . "<br>";
}
}
displayCheckboxes();
?>
function displayCheckboxes($img_array){
foreach($img_array as $img) {
echo "<img src='".$img."'>'" . "<br>";
}
}
and
echo displayCheckboxes($data);
There is several errors with your function :
- $data is not inside the function
- .jpg is already in the image name.
this should work,call the function with $data
$data = array("images/architecture-57e8d34a48_640.jpg",
"images/gateway-arch-57e2d64548_640.jpg",
"images/horseshoe-bend-57e6d6434f_640.jpg",
"images/lake-irene-57e6d24a4d_640.jpg",
"images/silhouette-57e8d5444e_640.jpg");
/* Write your displayCheckboxes() function here */
displayCheckboxes($data);
function displayCheckboxes($data){
for ($i=0; $i<count($data); $i++) {
echo "<img src='".$data[$i]."'.jpg>'" . "<br>";
}
}
?>
<?php
$data = array(
"images/architecture-57e8d34a48_640.jpg",
"images/gateway-arch-57e2d64548_640.jpg",
"images/horseshoe-bend-57e6d6434f_640.jpg",
"images/lake-irene-57e6d24a4d_640.jpg",
"images/silhouette-57e8d5444e_640.jpg"
);
?>
<body>
<main id ="main">
<form id="pics" action="process.php" method="get">
<label>Name: </label>
<?php foreach($data as $image): ?>
<img src="<?= $image; ?>"></br>
<?php endforeach; ?>
</form>
</main>
</body>
This is an alternative way. Looks much cleaner and readable. And I actually don't think we need a function for printing HTML.
I would say NEVER put HTML inside PHP echo unless it is compulsory. The reason I am stating this is if you put HTML inside PHP, the code becomes easily messy. It becomes hard to understand the logic unless you are the one who coded. Particularly, if you are working with some designers or in future, they will have a hard time making even little changes.
Even I think using <?= than <?php echo is better.
You are trying to give an extension ".jpg" of the images that you previously gave to it in the path of images in the array "$data".
For exemple:We will take the first element in the array $data with index = 0 "$data[0]" (images/architecture-57e8d34a48_640.jpg):
In PHP looks like -> echo "<img src='".$data[0]."jpg'>" . "<br>";
,but in HTML looks like -> <img src='images/architecture-57e8d34a48_640.jpg.jpg'>
You shold have to delete the extension ".jpg" from your echo to seem like:
echo "<img src='".$data[$i]."'>" . "<br>";

wrong window output

hello i have a problem everytime i submitted a form . This is script i made
<?php include 'rumahtable.php';
include 'connect.php';
$result = $conn -> query("select kodhomestay from rumah");
?><br><br>
<form action="" method="POST">
<label>Memadam data rumah : </label>
<select name="rumah">
<option>--Pilih Kod Rumah--</option>
<?php while ($rows = $result->fetch_assoc()){
$jrumah = $rows['kodhomestay'];
echo "<option value='$jrumah'>$jrumah</option>";
} ?>
</select><br><br>
<button type="submit" name="submit" >Delete</button>
<button type="submit" name="update">Update</button>
</form>
<?php if(isset($_POST["submit"])){
include 'deletedata.php';
}
if(isset($_POST["update"])){
ob_clean();
$rumah = $_POST["rumah"];
include 'updatedata.php';
}?>
and this is the deletedata.php:
<?php include 'connect.php';
$rumah=$_POST["rumah"];
$query = "delete from rumah where kodhomestay = '$rumah'";
$delete= mysqli_query($conn,$query);
if($delete){ ?>
<script type="text/javascript">
window.alert("Data Berjaya Dipadam.");
</script>
<?php header("Refresh:0");
}else{ ?>
<script type="text/javascript">
window.alert("Maaf data tidak dapat dipadamkan atau data tidak wujud.");
</script>
<?php header("Refresh:0");
}
and this is updatesenarai.php:
<?php $rumah = "RH007";
if(empty($_POST['submit'])){ ?>
<h2>Kemaskini Data Rumah</h2>
<?php include 'updaterumahtable.php'; ?>
<form action="" method="POST">
<label>Kod Rumah : </label><?php echo $rumah ?><br><br>
<label>Nama Rumah : </label><input type="text" name="jrumah"><br><br>
<label>Harga : </label><input type="number" name="price"><br><br>
<input type="submit" name="submit" value="Kemaskini">
<form>
<?php }else{
include 'connect.php';
$jr = $_POST['jrumah'];
$price = $_POST['price'];
$query = "update rumah set jenishomestay='$jr', harga=$price where kodhomestay='$rumah'";
$update= mysqli_query($conn,$query);
if($update){?>
<script type="text/javascript">
window.alert("Data Berjaya Dikemaskini.");
</script>
<?php header("Refresh:0");
}else{ ?>
<script type="text/javascript">
window.alert("Maaf data tidak dapat dikemaskini atau data tidak wujud.");
</script>
<?php header("Refresh:0");
}
mysqli_close($conn);
}?>
everytime i click the update button. it uses the updatesenarai.php but when i want to update the data in sql. it wont update and keep saying the first window alert in delete data. But when i used only updatedata.php it works just fine. If i change the submit button name it the whole file refreshes even without the header(). I tried changing the $conn into something else but it still saying the first window alert from deletedata.php. Im still new.
Check your updatesenarai.php. Your deletedata.php and updatesenarai.php are exactly same. Please write the code for updating in updatesenarai.php.

Ajax fresh input field generated by PHP DOM

I am a student studying Ajax to change the value of an input field generated with PHP DOM.
Here is the code that generates the table rows.
<?php
$totalval = 0;
foreach($_SESSION as $name=>$value){
$total = 0;
if(substr($name,0,5) == "item_"){
$id = substr($name,5,4);
$cart_xml= new DomDocument;
$cart_xml->Load('prod_db.xml');
$root=$cart_xml->getElementsByTagName('root')->item(0);
$product=$root->getElementsByTagName('product');
foreach($product as $prod){
$itms_id=$prod->getElementsByTagName('prod_id')->item(0)->nodeValue;
$itms_categ=$prod->getElementsByTagName('prod_categ')->item(0)->nodeValue;
$itms_imgsrc=$prod->getElementsByTagName('prod_imgsrc')->item(0)->nodeValue;
$itms_name=$prod->getElementsByTagName('prod_name')->item(0)->nodeValue;
$itms_price=$prod->getElementsByTagName('prod_price')->item(0)->nodeValue;
$itms_stock=$prod->getElementsByTagName('prod_stock')->item(0)->nodeValue;
if($id==$itms_id){
$price = floatval($itms_price);
$total=$price*$value;
?>
<tr>
<td><img src="<?php echo $itms_imgsrc?>" class="citemimg"/></td>
<td>
<p class="prodname"><?php echo ucfirst($itms_name);?></p>
<p class="prodcateg"><?php echo ucfirst($itms_categ)?></p>
<p class="prodprice"><?php echo "Php. ".number_format($itms_price,2)?></p>
</td>
<td class="cartAmount;">
<img onclick="ajaxMinus('<?php echo $itms_id;?>','<?php echo $itms_stock;?>')" class="carticons" src="images/logos/Minus_r_50px.png"/>
<input type="number" min="1" max="<?php echo $itms_stock;?>" class="cartqty" id="itmQty<?php echo $itms_id;?>" value="<?php echo $value; ?>" disabled>
<img onclick="ajaxAdd('<?php echo $itms_id;?>','<?php echo $itms_stock;?>')" class="carticons" src="images/logos/Add_r_50px.png"/>
</td>
<td><?php echo "Php. ".number_format($total,2);?></td>
<td>
<a href="cart_actionCart.php?del=<?php echo $itms_id;?>&maxval=<?php echo $itms_stock;?>">
<img class="carticons" src="images/logos/Remove_r_50px.png"/>
</a>
</td>
</tr>
<?php
}
}
$totalval+=$total;
}
}
?>
And I make my image tag clickable by adding
`onclick="ajaxMinus('<?php echo $itms_id;?>','<?php echo $itms_stock;?>')"`
And I add as well
`onclick="ajaxAdd('<?php echo $itms_id;?>','<?php echo $itms_stock;?>')"`
And here is my simple function for ajaxAdd
function ajaxAdd(id,maxqty){
if(window.XMLHttpRequest){
xhrAdd=new XMLHttpRequest();
}
else{
if(window.ActiveXObject){
try{
xhrAdd=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){}
}
}
if(xhrAdd){
xhrAdd.onreadystatechange=minusQty;
xhrAdd.open("GET","cart_actionCart.php?add="+id+"&maxval="+maxqty,true);
xhrAdd.send();
}
else{
alert("Couldn't create an XMLHttpRequest");
}
}
function minusQty(){
if(xhrAdd.readyState == 4 && xhrMinus.staus == 200){
document.getElementById('itmQty'+id).value = xhrAdd.responseText;
}
}
This is working but my problem is the input value is not changing until I refresh the page, where is my problem? Any help is greatly appreciated.
Anything loaded in via AJAX will not have a listener unless the parent element was there in the first place.
As an example, I had a form HTML come through AJAX, and the datepicker would not work, until I changed the original code from this:
$('.datepicker').datepicker();
To this:
$("body").on("focusin", ".datepicker", function(){
$(this).datepicker();
});
Because body is there always, the listener now works. This will probably what your issue is too, so I suggest trying to adapt your listeners as above.

Return to Home page after Delete function PHP MYSQL JAVASCRIPT

Sorry I'm Beginner, I would like to ask if how to go back from Home page or Specific Page after Delete.php it was only a blank page.
EDIT
It's look like this after the delete was succesful.
Here is my code:
delete.php
<?php
session_start();
include_once 'dbconnect.php';
$id = $_GET['id'];
$sql = mysql_query("DELETE FROM accounts where user_id='$id'");
if ($sql > 0) {
?>
<script> alert('Record has been successfully deleted'); </script>
<?php
} else {
?>
<script> alert('Database Error!'); </script>
<?php
}
?>
Here is where I call the delete.php function:
<td>
<a href="delete.php?id=<?php echo $row['user_id']; ?>" onclick="return confirm('Are you sure?');">
Delete
</a>
</td>
Use javascript window.location
<?php
session_start();
include_once 'dbconnect.php';
$id=$_GET['id'];
$sql=mysql_query("DELETE FROM accounts where user_id='$id'");
if($sql > 0) {
?>
<script> alert('Record has been successfully deleted');
window.location.replace("page name");
</script>
<?php
} else{
?>
<script> alert('Database Error!');
window.location.replace("page name");
</script>
<?php
}
?>
You can use
header('Location:any_page.php');
exit;
in delete.php

Deleting a profile request?

I am a newbie trying to delete a profile but this message pops up in address bar
http://thexyz.com/the/delete.php?id=1>
Am I making any syntax mistake my code is as below-
<?php
$i=1;
while($row=mysql_fetch_array($rw)) { ?>
<tr>
<td><?php echo $i; ?></td>
<td align="center">
<a href="delete.php?id=<?php echo $row['Customer Id']; ?> "onclick="return chkstatus();">
<img src="resources/images/icons/user_delete.png" width="16" height="16"/></a>delete
</td>
Even delete is not working
My delete.php code is
<?php
session_start();
if($_SESSION['username']=="");
include_once('db.php');
if( isset($_GET['del']) ) {
$id = $_GET['del'];
$sql= "DELETE FROM opd WHERE Sno='$Sno'";
$res= mysql_query($sql) or die("Failed".mysql_error());
echo "<meta http-equiv='refresh' content='0;url=index.php'>";
}
?>
You have to remove the > after ?>
Your <a href should look like this:
<a href="delete.php?id=<?php echo
$row['Customer Id']; ?>" onclick="return chkstatus();">
In your delete.php
if($_SESSION['username']==""); // IT will do nothing here
above code will not do anything because your have terminated statement with semicolon ;
You are passing id not del.
so $_GET['del'] should be changed as $_GET['id']
$sql= "DELETE FROM opd WHERE Sno='$Sno'";
changed to
$sql= "DELETE FROM opd WHERE Sno='$id'";

Categories

Resources