I am trying to update a HTML DIV dynamically while updating some information in a MySQL database, without reloading the page. How do I get this HTML DIV to update?
echo "<td id=\"trade_refs_update\">".(($results[$k]["trade_references_supplied"] == "Yes") ? "<img src=\"images/checkmark.png\" width=\"20\" height=\"20\" />" : "")."</td>";
Here's the PHP code:
if(isset($_REQUEST["edit_app"]))
{
$columnname=$_REQUEST["column"];
$value=$_REQUEST["editval"];
if ($set_trade_references == 'Yes') {
$sql_update=('UPDATE apps_data set `'.$columnname.'` = "'.$value.'", "trade_refs" = "Yes" WHERE app_id='.$app_id.';');
echo "<script>$('#\"trade_refs_update\"').html('\"<img src=\"images/checkmark.png\" width=\"20\" height=\"20\" />\"');</script>";
} else {
$sql_update=('UPDATE apps_data set `'.$columnname.'` = "'.$value.'" WHERE app_id='.$app_id.';');
echo "<script>$('#\"trade_refs_update\"').html('\"<img src=\"images/error.png\" width=\"20\" height=\"20\" />\"');</script>";
}
$results = $db_handle->executeQuery($sql_update);
if ($DEBUG =='On'){
echo "columnname: ".$columnname."<BR>";
echo "value: ".$value."<BR>";
echo "sql_update: ".$sql_update."<BR>";
}
}
I think you're searching for Ajax! :)
Related
I have a question by these given information:
private function loadIntervallOptions($intervallid = 1, $intervallfactor=1){
echo "<select name='intervallfactor'>";
for($intervallnumber=1;$intervallnumber<10;$intervallnumber++){
$checked = "";
if($intervallnumber== $intervallfactor){
$checked = "selected";
}
echo "<option $checked>$intervallnumber</option>";
}
echo "</select>";
echo "<select name='intervallid'>";
for($i=0;$i<count($this->todo_intervall);$i++){
echo "<option value='{$this->todo_intervall[$i]->getintervallid()}' ";
if($this->todo_intervall[$i]->getIntervallid() == $intervallid){
echo "selected";
}
echo ">{$this->todo_intervall[$i]->getIntervall()}</option>";
}
echo "</select>";
}
What I want to do:
If intervallid=1 I want to hide the select 'intervallfactor'. Is this possible?
Thank you in advance!
EDIT:
I'm trying this but it won't work
$('.intervallid').change(function(e) {
if ($('.intervallid').val()==1){
$('.intervallfactor').attr('disabled','disabled');
}
else{
$('.intervallfactor').removeAttr('disabled');
}
});
$('.intervallid').trigger('change');
You are trying to reference the html element using a class name .intervallid which doesnt exist.
either you should use $('select[name="intervalid"]) or add an id to the select from php and, use that to reference the element.
PHP
echo "<select name='intervallid' id='intervall_id'>";
JS
$('#intervall_id').attr('disabled', 'true')
I've tried searching everywhere(the past 2 hours), and I've tried all these different solutions and nothing has worked for me thus far. So I thought I would come here and ask the geniuses that be. Since my Jquery isn't very good. I know it has something to do with id's and such not loaded when the script is up, but I don't know how to go about fixing this.
The code I'm about to show I'm certain isn't the best. I've only been doing this for a few months. So please be patient with me.
Below is the main.php file:
echo "<div class=\"main_menu_buttons\">";
echo "<table id=\"nav\">";
echo "<tr><td>";
echo "<ul id=\"nav\">";
echo "<li><button class=\"pm_button\" onclick=\"masspm('new', 'menu')\">New</button></li>";
echo "<li><button class=\"pm_button\" onclick=\"masspm('edit', 'menu')\">Edit</button></li>";
echo "<li><button class=\"pm_button\" onclick=\"masspm('view', 'menu')\">View</button></li>";
echo "</ul>";
echo "</td></tr>";
echo "</table>";
echo "</div>";
echo "<span id=\"wait_1\" style=\"display: none;\">";
echo "<img alt=\"Please Wait\" src=\"/opserv/regimental/ajax-loader.gif\"/>";
echo "</span>";
echo "<span id=\"result_1\">";
// This is the default load screen. This will change every time we click a button.
echo "<div class=\"editor\">";
echo "<br><span class=\"header\">List of your PM groups:</span><br>";
$resultVB = $mysqliVB->query("my query");
if ($resultVB->num_rows > 0){
while ($rowVB = $resultVB->fetch_assoc()){
echo " - ".$rowVB['name']."<br>";
}
}else{
echo " - You have no groups.<br>";
}
echo "</div>";
// End of Default Load screen
echo "</span>";
-------
//Here is the script for loading ajax
<script type="text/javascript">
function masspm(type, arg, global) {
$('#wait_1').hide();
$('#wait_1').show();
$('#result_1').hide();
$.get("/forum/opserv/ajaxselect.php", {
func: type,
drop_var: arg
}, function(response){
$('#result_1').fadeOut();
setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400);
});
return false;
}
function finishAjax(id, response) {
$('#wait_1').hide();
$('#result_1').show();
$('#'+id).html(unescape(response));
$('#'+id).fadeIn();
}
</script>
I used to have the validation file in this file. But was told not to put it in this file and use it in my ajax called file which is ajaxselect.php
Now here is the ajaxselect.php
echo "<div class=\"editor\">";
echo "<br><span class=\"header\">Create New PM Group:</span><br>";
echo "<form name=\"new\" action=\"/forum/opserv/addon_pmgroup.php?op=addpmgroup\" method=\"post\" onSubmit=\"return validate();\">";
echo "Group Name: <input type=\"text\" name=\"name\"><br><br>";
echo "<table border=1 class=\"pmTable\"><tbody><tr>";
$i = 0;
while ($rowVB = $resultVB->fetch_assoc()){
if ($i > $rows){
echo "</td><td>";
$i = 0;
}elseif ($i == 0){
echo "<td>";
}
echo "<input type=\"checkbox\" id=\"member".$rowVB['userid']."\" name=\"members[]\" class=\"aar-checkbox\" value=\"".$rowVB['userid']."\"/><label for=\"member".$rowVB['userid']."\" class=\"aar-label\"></label> ".$rowVB['username']."<br>";
$i++;
}
echo "</tbody></table>";
echo "<br><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Submit\"/>";
echo "</form>";
echo "</div>";
And then here I have the validation script which is also in the ajaxselect.php
<script type="text/javascript">
function validate(){
msg = ""
if(document.new.name.value.length < 3){
msg += "Please enter a valid name\n";
}
if ($("#new input:checkbox:checked").length == 0) {
msg += "Please select a member\n";
}
if ($("#new input:checkbox:checked").length > 30 ) {
msg += "You many only have 30 members per group\n";
}
if(msg != ""){
alert(msg);
return false;
}else{
return true;
}
}
</script>
I'm pretty sure its possible, but I'm lost as to how to make it actually validate the info.
If someone would be so kind, I would appreciate any help.
I was wondering if it's possible to have an if statement within an echo.
I have if statement which works fine when echoing results through the a while loop... This is the statement:
<div><?php if ($row['image'] == '') {}
else {echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";} ?>
<?php if ($row['video'] == '') {}
else {echo "<iframe src={$row['video']}></iframe>";} ?></div>`
So basically it's either a video or an image which works fine but then I implemented an infinite scroll to my blog which echoes the data from the database through and if statement like so:
if ($results) {
while($obj = $results->fetch_object())
{
echo '
<div><h3>'.$obj->headline.'</h3> </div>
<div><img src='data:image/jpeg;base64,".base64_encode('.$obj->image.')."'></div>'
So I wondering if anyone knows if it's possible to transfer that if statement within this echo so that it display an image firstly and then knows whether one is present or when a video is present within the database.
Thanks in advance for any help.
PS: I'm very new to coding/php!
Of course. Just split up the echo into multiple statements:
while($row = $results->fetch_object()) {
echo '<div>';
if ($row['image'] == '') {
} else {
echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";
}
if ($row['video'] == '') {
} else {
echo "<iframe src={$row['video']}></iframe>";
}
echo '</div>';
}
Try this one.
//first initialize a variable as a string
$result="";
while($obj = $results->fetch_object()) {
$result.="<div>";
if (!empty($obj['image'])){
$result.="<img src='data:image/jpeg;base64,".base64_encode($obj['image'])."'>";
}
elseif (!empty($obj['video'])){
$result.="<iframe src={$obj['video']}></iframe>";
}else{
//show some notification or leave it
//echo 'not Found';
}
$result.="</div>";
}
//finally you need to print the result variable.
echo $result;
I'm currently making a droplist but in the droplist let's say I only want to include only .txt extension files so any other extensions like .php .jpg or any other extensions will not be in in the droplist. How can I do that as simple as possible?
Another question is I want to make a warning IF the folder does not have any .txt extension files an error message will show. So even if there are other .jpg .php or any other files inside as long as there's no .txt file in the folder a warning will show.
Anyone able to give me a hand?
This is what I have done but it only shows a drop-list with no .txt at the end but it will still show other random files in the drop-list though.
if(!(is_dir("./aaa")))
{
die("Must create a folder first, sorry");
}
$lists = scandir("./aaa");
echo "<form action=\"./page2.php\" method=\"get\">";
echo "<select>";
foreach($lists as $list)
{
if(($list == ".") || ($list == ".."))
{
continue;
}
echo "<option value=\"";
echo basename($list,".txt");
echo "\">";
echo basename($list,".txt");
echo "</option>";
}
echo "</select>";
echo "</form>";
editted added the substr with $hasTxt
<?php
if(!(is_dir("./aaa")))
{
die("Must create a <strong>aaa</strong> folder first, sorry");
}
echo "<form action=\"./page2.php\" method=\"get\">";
echo "<select name=\"aaa\">";
$aaa_files = scandir("./aaa");
$hastxt = false;
foreach($aaa_files as $file_list)
{
if(($file_list == ".") || ($file_list == ".."))
{
continue;
}
if(strlen($file_list)>4 && strtolower(substr($file_list, -4))!='.txt')
{
continue;
}
else
{
$hastxt = true;
echo "<option value=\"";
echo basename($file_list,".txt");
echo "\">";
echo basename($file_list,".txt");
echo "</option>";
}
}
echo "</select>";
echo "<br/><input type=\"submit\">";
echo "</form>";
if($hastxt == false)
{
echo "Must create text files first, sorry";
die();
}
?>
This is what happens for the script that I have now if the folder does not have any txt files.
This is what I actually want if there's no txt file but of course without the arrow
For the first part, just like you continue on directories . and .., you can continue on non-text files:
if(strlen($list)>4 && strtolower(substr($list, -4))!='.txt') continue;
For the warning part, put a flag before the foreach
$hasTxt = false;
And set it to true whenever you get input you don't ignore (ie. after the if(unwanted) continue;)
$hasTxt = true;
Finally, after the foreach check the value of $hasTxt and use it as you prefer.
You could use PHP's substr() function to test the filenames:
if(substr($filename, -3) == 'txt') {
// show file
}
See here: http://php.net/manual/en/function.substr.php
Try this , Hope it will work you
<?php
if(!(is_dir("./aaa")))
{
die("Must create a folder first, sorry");
}
$lists = scandir("./aaa");
$i =0;
foreach($lists as $list)
{
if (strstr($list, '.txt')) {
$i++;
}
}
if($i == 0){
die("the folder does not have any .txt extension files");
}
echo "<form action=\"./page2.php\" method=\"get\">";
echo "<select>";
foreach($lists as $list)
{
if (strstr($list, '.txt')) {
echo "<option value=\"".substr($list,0, -4)."\">".substr($list, 0,-4)." </option>";
}
}
echo "</select>";
echo "</form>";
?>
is there a way to speed up my code? It takes about 15 seconds to load ... I don't really see a way to reduce my code... I just thought about inserting the values into database, so the user does not have to load new info every time.. but the thing is that my cron only allows 1 load per hour ... by loading new info on every load it gives me fresh information..
$q1=mysql_query("SELECT * FROM isara");
while($r1=mysql_fetch_array($q1)){
$named=$r1['name'];
$idd=$r1['id'];
$descd=$r1['desc'];
$online=check_online($named);
$char = new Character($r1['name'],$r1['id'],$r1['desc']);
if($online == "online"){
$char->rank = $i++;
}
else{
$char->rank = 0;
}
$arr[] = $char;
}
?>
<br />
<h2 style="color:green">Online enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank>=1){
echo "<a style=\"color:green\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
<br />
<h2 style="color:red">Offline enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank==0){
echo "<a style=\"color:red\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
As I wrote in the comment, fetch the page once instead of once for every name in the database.
Pseudo code for my comment:
users = <get users from database>
webpage = <get webpage contents>
for (user in users)
<check if user exists in webpage>
As mentioned in the comments you're calling a webpage for each entry in your database, assuming that's about 2 seconds per call it's going to slow you down a lot.
Why don't you call the page once and pass the contents of it into the check_online() function as a parameter so your code would look something like this which will speed it up by quite a few magnitudes:
$content=file_get_contents("http://www.tibia.com/community/?subtopic=worlds&world=Isara",0);
$q1=mysql_query("SELECT * FROM isara");
while($r1=mysql_fetch_array($q1)){
$named=$r1['name'];
$idd=$r1['id'];
$descd=$r1['desc'];
$online=check_online($named,$content);
$char = new Character($r1['name'],$r1['id'],$r1['desc']);
if($online == "online"){
$char->rank = $i++;
}
else{
$char->rank = 0;
}
$arr[] = $char;
}
?>
<br />
<h2 style="color:green">Online enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank>=1){
echo "<a style=\"color:green\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
<br />
<h2 style="color:red">Offline enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank==0){
echo "<a style=\"color:red\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
and your check_online() function would look something like this:
function check_online($name,$content){
$count=substr_count($name, " ");
if($count > 0){ $ex=explode(" ",$name); $namez=$ex[1]; $nameused=$namez; }
else{ $nameused=$name; }
if(preg_match("/$nameused/",$content)){ $status="online"; }
else{ $status="offline"; }
return $status;
}
You can also do the following to make it faster
Stop using select * which is very bad on innodb
Put better indexes on your database to make the recordset return faster
Install PHP 5.4 as it's faster especially as you're creating a new object in each iteration
Use a byte code accelerator/cache such as xdebug
you should avoid using distinct (*) keyword in your SQL Query
for more information read this http://blog.sqlauthority.com/category/sql-coding-standards/page/2/