HTML onClick functionality on working - php

I am trying to get images from my server via php and I want to have onClick functionality but it is not working following is my code sample:
<?php
$conn = ftp_connect("myserver") or die("Could not connect");
ftp_login($conn,"username","password");
$images = ftp_nlist($conn,"folder");
$r = count($images);
for($i=0;$i<$r;$i++)
{
//echo " $images[$i] ";
echo"<img id= '$i' class = '' border='1' src='mysource' width='300' height='250'>";
echo "<button onClick= 'hide()'> Print </button>";
echo "<button> Email </button>";
echo "<button> Text Me </button>";
echo "</br>";
}
ftp_close($conn);
?>
and following is my javascript code
function hide()
{
var t = document.getElementById(x);
t.setAttribute(class, print);
}
when I click my print button it is not even calling that function by the way this all is in .php file. Thanks for ay help.

→ Try This:
echo "<script type='text/javascript'>function hide_it(){alert('Entering Function hide_it()?'); /*var t = document.getElementById(x); t.setAttribute(class, print);*/}</script>";
for($i=0;$i<$r;$i++)
{
//echo " $images[$i] ";
echo"<img id= '$i' class = '' border='1' src='mysource' width='300' height='250'>";
echo "<button type='button' onclick='javascript:hide_it()'> Print </button>";
echo "<button> Email </button>";
echo "<button> Text Me </button>";
echo "</br>";
}

Related

How to check table cell in database and output results?

I am bit new to php and sql.
I am simply reading from a database table and echoing images to a page. The images are then styled with a text aligned in the middle saying 'Play Video'.
In the table 'adcTable' some entries do not have a video stored.
I would like to have the rows/entries with video say 'Play Video' and the ones without just show the image.
Not to sure how bess to explain. Hope to get some assistance.
<php
$sql = "SELECT client_id, images, video FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["images"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Play Video</div>
</div>
</div>
</td>";
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
echo "</tr></table>";
?>
Thanks guys, I was able to use the example provided by BusinessPlanQuickBuilder to solve.
$sql = "SELECT client_id, files, file FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
if ($row['file'] == "VidUploads/"){
echo "<td>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
</td>";
echo '<script type="text/javascript">',
'$( ".text" ).css( "border", "3px solid red" );',
'</script>';
} else{
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Video</div>
</div>
</div>
</td>";
}
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
}
echo "</tr></table>";
?>
Use if empty condition on video column. This is the fundamental code, add your formatting as required.
<?php
while($row = $result->fetch_assoc()) {
if (empty ($row['video']) ) {
echo $row ["images"];
} else {
echo "Play Video";
}
}
?>
SO reference to related post

Link for same page

Longtime reader of stackoverflow but second question.
This is what i am doing
echo "<td><a href = 'http://localhost/map/index.php'
value='$id' >Delete</a></td>";
This is what we do when we use submit button
<?php
echo "<body style='background-color:gray'>";
if (isset ($_POST['submit'])){
include("connection.php");
echo " <table id='t01' border='20px solid black' width='100%' >";
echo "<tr>";
echo "<th> DETAILS ABOUT DELETION</th>";
echo "<td>DELETED SUCCESSFULLY </td></tr>";
$id = $_POST["submit"];
$select1="DELETE FROM app where id='$id'";
$result = $conn->query($select1);
if ($conn->query($result)==true){
echo "<td>DELETED SUCCESSFULLY </td></tr>";
}
echo "</table>";
//echo "DELETED SUCCESSFULLY";
}
?>
I just want to do something like that * without using input type=submit* ..... But dont know how to do
Any advice will be appreciated ... Thanks in advance
firstly pass the id in the url like so:
echo "<td><a href = 'http://localhost/map/index.php?id=$id'>Delete</a><td>";
then check it with:
if (isset ($_GET['id'])){
...
you basically swapping POST for GET

How can I pass ids to colorbox?

Colorbox is not recognizing ids of random elements when clicked on , it alerts ids in ascending order when you click on elements randomly .. How can I make it so the right id is displayed when clicking on whatever element ?
JavaScript Code
jQuery(document).ready(function() {
$("a.madscore").colorbox({
inline:true,
width:"350px",
href: "#madcomment_menu"
});
$("div#scoring_scale a").click(function(e) {
e.preventDefault();
ID = $(this).attr('id');
alert(ID);
point = $(this).text();
username = $('#username'+ID).val(); alert(username);
name = $('#name'+ID).val();
image = $('#image'+ID).val();
//var message = $('textarea#text'+ID).val(); alert(message);
var result ='Just gave #'+username+' a score of'+point+'via MadFlock';
$('textarea#text'+ID).attr("value", result);
tweet = $('textarea#text'+ID).val();
});
});
PHP Code
<?php
$select = "SELECT * FROM COMMENTS INNER JOIN Twitter_Data ON Twitter_Data.screen_name=Comments.Twitter WHERE Category ='Comments'";
$result = mysql_query($select);
$result_count = mysql_num_rows($result);
echo " <table border =\"0\">";
echo "<tr>";
$user_array = array();
$counter = 0;
if($result_count > 0) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<div id ='scoring_scale' class='madscore".$row['ID']."' style='display:none;'>";
echo "<div id='madcomment_menu' style='padding:10px; background:#fff;'>";
echo "<a id='".$row['ID']."' class='green_circle' href='#'> +3 </a>";
echo "<a id='".$row['ID']."' class='orange_circle' href='#'> +1 </a>";
echo "<a id='".$row['ID']."' class='red_circle' href='#'> -1 </a>";
echo "<a id='".$row['ID']."' class='brown_circle' href='#'> -3 </a><br />";
echo"<form>";
echo "<textarea id='text".$row['ID']."'rows='5' cols='33'>";
echo "-";
echo "</textarea>";
echo"<button id='button".$row['ID']."'class='button_madscore'> MadScore </button>";
echo "</form>";
echo "</div>";
echo "</div>";
}
}
// Here is the link that will generate the COLORBOX pop-up
echo "<a id='".$row['ID']."'class=' madscore' href='#madcomment_menu'><img src='images/madcomment.png' /> </a>";
?>

Set approval for admin with checkbox using php,jquery and ajax

I am working in a project where i want to show only those products to user which are selected by admin from database. Actualy i want to set the approval 1 or 0 in database when admin check or unchecked that checkbox.
jobs.php
$(document).ready(function(){
$('input.check').click(function(){
if($("input.check").is(':checked'))
{
$id = $(this).attr("id");
$.post("handle.php",{action:"checked",id:$id},function(data){
alert("Peoduct is set to display...");
});
}
else
{
alert("unchecked");
$id = $(this).attr("id");
$.post("handle.php",{action:"unchecked",id:$id},function(data){
alert("Peoduct is un-set to display...");
});
}
});
});
<?php
$dbqry = mysql_query("select * from job_category");
echo "<table width='50%', border='2'>
<tr>
<th>Catergory ID</th>
<th>Category Name</th>
<th>Remove</th>
<th>Update</th>
<th>Approval</th>
</tr>";
if($dbqry)
{
while($row = mysql_fetch_array($dbqry))
{
echo "<tr>";
echo "<td align='center'>" . $row['c_id'] . "</td>";
echo "<td align='center'>" . $row['cat_name'] ."</td>";
$cid = $row['c_id'];
$aprv = $row['approval'];
echo "<td align='center'><a href='remove.php?action=cat_remove&cid=$cid'>Remove</a></td>";
echo "<td align='center'>
<a href='Update-form.php?action=cat_update&cid=$cid'>Update</a></td>";
?>
<td align="center">
<input type='checkbox' name='approval' value='approval' id ="<? echo $cid; ?>" class="check"/>
</td>
</tr>
<?
}
echo "</table>";
echo '<br/>';
echo "<a href='add.php?action=cat_add'>Add New Category</a>";
}
else
{
die(mysql_error());
}
?>`
handle.php
`<?php
include 'include/connection.php';
$action = $_POST['action'];
$id = $_POST['id'];
//echo $action;
if($action == "checked")
{
$query = "update job_category set approval=1 where c_id=$id";
$result = mysql_query($query);
if(!$result)
{
echo die(mysql_error());
}
}
else if($action == "unchecked")
{
$query = "update job_category set approval=0 where c_id=$id";
$result = mysql_query($query);
if(!$result)
{
echo die(mysql_error());
}
}
?>`
Its working but when i refresh the page or seletc the URL and press enter then all the checked data appears unchecked even after that it does not change value of approval from 1 to 0 in database, but still it make me confuse about which items are checked or unchecked. Any suggestion will be appreciated.
modify checkbox line to show checked if approval=1 in database... try this
<input type='checkbox' name='approval' value='approval' <?php if($row["approval"]==1){ echo "checked=\"checked\"";}) ?> id ="<? echo $cid; ?>" class="check"/>
html
<input type='checkbox' name='approval' value='approval' <?php echo $row["approval"]?"checked=\"checked\"":'';?> id ="<? echo $cid; ?>" class="check"/>
then add js
$(function(){
$('input[type=checkbox]').each(function(){
if($(this).attr('checked') && $(this).attr('checked')=='checked'){
$(this).attr('checked', true);
}else{
$(this).attr('checked', false);
}
})
})
and
if($("input.check").is(':checked'))
to
if($(this).is(':checked'))

Updating content to the database onclick event

I spent the last 2 days trying to make the following code work.
I have three forms with content in different languages. For each of them there is an Edit button which popup a text editor. What I would like to do is when I click the button "Save and close" the edited content would be updated in the database using javascript (AJAX) and PHP/SQL but in the console it gives me the following error: Uncaught ReferenceError: valueContent is not defined. So here is the code I have so far.
$mvccont .=
"<form action='edit_language/".ID.".raw' method='post' target='pop' class='comparelist' style='clear: both' onsubmit='window.open(\"\", \"pop\", \"width=0,height=0,resizeable,scrollbars\");'>".
"<input type='hidden' name='keyword' value='".$keyword."'>".
"<table style='width:100%; border-collapse: collapse;' >".
"<tr><td width='20%' ><div class='info' style='width:100%; border:none;' >".
substr($keyword, 0, 20)."<a title='".$keyword."'><input type='submit' value='Save' class='BUYN'/></a>".
"<a href='edit_language/".$keyword."/delete'><input type='button' value='Delete' class='NONA'/></a>".
"</div></td>";
foreach($lan as $lang=>$lame) {
$idk = str_replace(" ", "", $keyword).$lang;
$mvccont .=
"<td><div class='number3'".(($red[$keyword][$lang]=='')?" style='background: #fcc;'":"")." style='border:none;' >".
"<input type='hidden' name='touch".$lang.$keyword."'/>".
"<input type='text' id='s".$idk."' name='a".$lang."' value='".htmlentities($lame, ENT_QUOTES, "UTF-8")."' onchange=\"this.style.borderColor='#ff0000'; document.getElementsByName('touch".$lang.$keyword."')[0].value='true'\"/><a onclick='popbox(false, ".$idk.", true); mce();'><input type='button' value='Edit' class='BUYN'/></a>".
"<br />".($red[$keyword][$lang]).
"<script>".
"function falaffel".$idk."(step) {".
"var l".$idk."=document.getElementById('s".$idk."').value;".
"if(step==1) {".
"return '<textarea>'+l".$idk."+'</textarea><input type=\"button\" value=\"Save & close\" class=\"NONA\" onclick=\"'+\"document.getElementById('s".$idk."').value=tinyMCE.activeEditor.getContent(); ".$idk."=falaffel".$idk."(1); popboxremove(); document.getElementById('s".$idk."').style.borderColor='#ff0000'; updateValue(); document.getElementsByName('touch".$lang.$keyword."')[0].value='true' \"+'\">';".
"}".
"return l".$idk.";".
"}".
"var ".$idk." = falaffel".$idk."(1); ".
"</script>".
"</div></td>";
}
$mvccont .=
"</tr></table></form>";
Then here is the AJAX code:
function updateValue(){
var ajaxRequest = getXmlHttpRequestObject();
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var valueContent = document.getElementsByTagName("input");
}
}
ajaxRequest.open("POST", "comp/update_lang.php", true);
ajaxRequest.send(valueContent);
}
And finally the PHP code:
<?php
foreach(array(1=>"en", 2=>"it", 3=>"pl") as $id=>$lang) {
if($_POST["touch".$id.$_POST["keyword"]]=="true") {
$sql = "UPDATE translat SET value='".mysql_real_escape_string($_POST["a".$id])."', date=".NOWTIME." WHERE lang=".$id." AND keyword='".urldecode($_POST["keyword"])."'";
$insert = $db->query($sql);
$insert->execute();
#unlink(TEMP."lang_".$lang.".cache");
}
}
$mvccont .=
"<script>".
"window.close();".
"</script>";
die($mvccont);
?>
Thank you so much for your help.

Categories