Showing posts with label PHP strtolower. Show all posts
Showing posts with label PHP strtolower. Show all posts

Friday, 2 August 2019

Thursday, 30 August 2018

My ajax call does not pull dynamic data from PHP / Mysql


I am working on a project where I am going to have divisions from a league listed as buttons on a page. And when you click on a button a different team list shows for each division. 
All divisions and teams are stored in a mysql database and are linked together by the "div_id". The plan was have the buttons use javascript or Jquery to send the 'div_id" to a function; which would then use ajax to access an external php file and then look up all the teams for that division using the div_id and print them on the page. I have been piecing this all together and getting the various pieces to work. But when I put it all together; it seems like the ajax part - does not pull in fresh data from the database if the data is changed. In fact, if I change the PHP file to echo some more data or something, it keeps using the original unaltered file. So, if the data is changed that is not updated, and if the file is changed that is not updated. I did find if I actually copied the file with a new name and then had my ajax call use that file instead; it would run it with new code and the new data at that time. But then everything is now locked in at that point and cannot get any changes.

So - I do not know much about ajax and trying to do this. I am not sure if this is totally normal for what I am using and for a dynamic changing team list, it cannot be done this way with ajax calling a PHP file.
OR - maybe there is something wrong with the ajax code and file I have which is making it behave this way? I will paste in the code of my ajax code and also the php file…
here is the ajax call:
var answer = DivId;
$.ajax({
type: 'GET',
url:  'path_to_file/gscript2.php',
data: 'answer=' + answer,
success: function(response) {
    $('#ajax_content').html(response);
}
});

and here is the script.php file that it calls (removed db credentials):
<?php
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
    && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
) {
    // AJAX request
    $answer = $_GET['answer'];
    $div_id=$answer;

    echo "div id is: " . $div_id . "<br/>";

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database!       Please try again later.');
mysql_select_db($dbname);

$result_g1 = mysql_query("SELECT * FROM teams WHERE div_id=$div_id");

while($row = mysql_fetch_array($result_g1, MYSQL_BOTH))
{
$team_id=$row[team_id];
$team_name=$row[team_name];
echo $team_id . " " . $team_name . "<br/>";
}

}
?>

So - to sum up - is there something wrong with this making it do this? Or is what it is doing totally normal and I have to find a different way?
Thanks so much...

Most likely your browser is caching.
Try adding cache: false as such:
$.ajax({
    cache: false,
    type: 'GET',
    ...

The jQuery documentation explains that by doing so, it simply adds a GET parameter to make every request unique in URL.
It works by appending "_={timestamp}" to the GET parameters.

Monday, 20 July 2015

PHP Functions for Words Formatting

PHP functions are used for text formatting. Formatting like uppercase, lowercase and first letter uppercase can also be done using php functions, here is the 5 php functions for words/text formatting
  • ucwords
  • strtoupper
  • strtolower
  • ucfirst
  • lcfirst
ucwords()
Make each word’s first letter as capital letter
1
2
3
4
$text = "freeze coders";

$text1 = ucwords($text)// Output -  Freeze Coders

strtoupper()
strtoupper php function will make all words as upper case(capital letter)
1
2
3
4
$text = "freeze coders";

$text1 = strtoupper($text)// Output -  FREEZE CODERS

strtolower()
strtolower php function will make all words as lower case(capital letter)
1
2
3
4
$text = " FREEZE CODERS";

$text1 = strtolower($text)// Output -  freeze coders

ucfirst()
ucfirst php function will make the first letter of a string as uppercase
1
2
3
4
$text = "freeze coders";

$text1 = ucfirst($text)// Output -  Freeze coders

lcfirst()
lcfirst() php function will return the string’s first letter lowercase
1
2
3
4
$text = " FREEZE CODERS";

$text1 = lcfirst($text)// Output -  fREEZE CODERS

Thursday, 4 June 2015

PHP: PHP4 AND MySQL Authentication

<?php
require ("auth.php");
if(!isset($PHP_AUTH_USER))  {
   Header("WWW-Authenticate: Basic realm=\"User Login\"");
   Header( "HTTP/1.0 401 Unauthorized");
    echo "You failed to provide the correct password....\n";
   exit;
}
   else {
   $con = mysql_pconnect ("$host", "$user", "$pass")  or die("Error: " . mysql_error());
   mysql_select_db ("$db");
   $user_id = strtolower($PHP_AUTH_USER);
   $result = mysql_query("SELECT password FROM signup " . "Where username = '$user_id'");
   $row = mysql_fetch_array($result);
if ($PHP_AUTH_PW != $row["password"])  {
   Header( "WWW-Authenticate: Basic realm=\"Login failed please try again!\"");
   Header( "HTTP/1.0 401 Unauthorized");
    echo "You failed to provide the correct password....\n";
   exit;
  }
}
?>

Wednesday, 3 June 2015

PHP: File Uploader

<?php
# @function upload_file
# 
# @param $field  string  the name of the file upload form field
# @param $dirPath string  the relative path to which to store the file (no trailing slash)
# @param $maxSize int   the maximum size of the file (in bytes)
# @param $allowed array  an array containing all the "allowed" file mime-types
#
# @return mixed  the files' stored path on success, false on failure.
function upload_file($field = '', $dirPath = '', $maxSize = 100000, $allowed = array())
{
 foreach ($_FILES[$field] as $key => $val)
  $$key = $val; 

 if ((!is_uploaded_file($tmp_name)) || ($error != 0) || ($size == 0) || ($size > $maxSize))
  return false; // file failed basic validation checks

 if ((is_array($allowed)) && (!empty($allowed)))
  if (!in_array($type, $allowed))  
   return false; // file is not an allowed type

 do $path = $dirPath . DIRECTORY_SEPARATOR . rand(1, 9999) . strtolower(basename($name));
 while (file_exists($path));

 if (move_uploaded_file($tmp_name, $path))
  return $path;

 return false;
}

// DEMO
/*
if (array_key_exists('submit', $_POST))
{
 if ($filepath = upload_file('music_upload', 'music_files', 700000, array('audio/mpeg','audio/wav')))
  echo 'File uploaded to ' . $filepath;
 else
  echo "An error occurred uploading the file... please try again.";
}
echo '   <form method="post" action="' .$_SERVER['PHP_SELF']. '" enctype="multipart/form-data">
   <input type="file" name="music_upload" id="music_upload" />
   <input type="submit" name="submit" value="submit" />
  </form>
 '; 
print_r($_FILES);  // for debug purposes
*/ 

?>
 
Refer to the commented section of code under the function labeled DEMO for usage. 

Monday, 23 February 2015

PHP: Capture the requested URI and Clean up global variables

<?php
$requestURI = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']';
echo $requestURI;
?>

This snippet is more of an example of how to convert strings to lowercase characters, and then clean them up for use in scripts, etc
<?php
$_POST["name"] = strtolower(stripslashes(trim(htmlspecialchars($_POST["name"])))); $_POST["message"] = strtolower(stripslashes(trim(htmlspecialchars($_POST["message"]))));
?>