PHP - Calling Javascript After Php Check?
I have a javascript function that shows an alert message, but I only want it to show after a certain check is done by PHP/Mysql first. I can't get it to work, so I'm sure I'm doing something wrong. My code is below. I'm guessing I can't have Javascript inside PHP? If that's true, how would I go about doing something like this?
Code: [Select] <?php $query = "SELECT paid from users WHERE userid={$_GET['userid']}"; $result = mysql_query($query, $connection); if (!$result) { die("Database query failed: " . mysql_error()); } else { $paidinfo = mysql_fetch_array($result); if ($paidinfo['paid'] == "no") { ?> <SCRIPT LANGUAGE="JavaScript"> window.onload=deadBeat; </script> <?php } else { //do nothing } } ?> This is the function... Code: [Select] <SCRIPT LANGUAGE="JavaScript"> function deadBeat() { alert("You still owe money for your entry fee to this pool. Please pay Gary soon.") } </SCRIPT> Similar TutorialsI am trying to call a javascript function from a php script. This is the approach I'm using. I have an alert in the function, just to confirm the function executes. Code: [Select] echo "<script type='text/javascript' src='http://localhost/Project/JScript/File.js'>"; echo "<script type='text/javascript'>functionName();</script>"; Is this the correct approach? Hi, I'm currently in the process of making my first website. The problem is that I'm trying to change the language the website will be in by pressing a button that will call the setlocale() function. But I can't seem to get it to work. From what I read on the internet you can't use php in javascript because it's serverside. But I can't really think of an alternative way of doing this. Any sugestions? This is my code btw for what it's worth. Code: [Select] <form action="SetLangEN()" > <input type="image" src="../images/ENflag.jpg" alt= "EN" width="20px" height="15px" onclick="SetLangEN()" /> </form> </form> </div> <script type="text/javascript"> function SetLangEN() { <?php setlocale(LC_ALL, "en_US"); ?> } </script> I define a Javascript in the <head> section of a .php file and call it from the <body> section with the following: echo "<script language='javascript'>function();</script>". The function displays a confirm box with the normal Cancel and OK buttons just fine, but I want to capture the function return of true or false for which button was selected. So far I haven't found a way to get return a value from the function. Any ideas??? Code: [Select] <?php class My { public function disp() { echo "ehllo"; } } ?> <html> <head> <script type="text/javascript"> function displaymessage() { document.write(My.disp()); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> </body> </html> HEllo I wanted to know wheter php function can be call in js functions. Please Help. Hi guys, I am having a tough time executing the code inside the javascript. Everything is working fine with the Php code and I am able to see the output of variable myValue too.. But the problem is nothing is being executed inside the javascript..Not even the first statement of the javascript..When I look at the output sent by the HTML to the browser, the output is what is listed after this code which is right at the bottom of this page. Please advise. Code: [Select] <html> <head> <title>AES (Rijndael) Encryption Test in JavaScript</title> <script src="aes-enc.js" type="text/javascript" language="JavaScript"></script> <script src="aes-dec.js" type="text/javascript" language="JavaScript"></script> <script src="aes-test.js" type="text/javascript" language="JavaScript"></script> <script type="text/javascript"> function doDecryption() { document.println("Inside Javascript"); var ct, key; var theForm = document.forms[0]; blockSizeInBits=128; keySizeInBits = theForm.keySize[theForm.keySize.selectedIndex].value; if (theForm.key.value.toLowerCase().indexOf("0x") == 0) theForm.key.value = theForm.key.value.substring(2); if (theForm.ciphertext.value.toLowerCase().indexOf("0x") == 0) theForm.ciphertext.value = theForm.ciphertext.value.substring(2); if (theForm.key.value.length*4 != keySizeInBits) { alert("For a " + keySizeInBits + " bit key, the hex string needs to be " + (keySizeInBits / 4) + " hex characters long."); if (theForm.key.select) theForm.key.select(); return; } if (theForm.ciphertext.value.length*4 != blockSizeInBits) { alert("For a " + blockSizeInBits + " bit block, the hex ciphertext string needs to be " + (blockSizeInBits / 4) + " hex characters long."); if (theForm.ciphertext.select) theForm.ciphertext.select(); return; } ct = hex2s(<?php echo $myValue; ?>); document.println("Inside Javascript"); document.write(ct); // key = hex2s(theForm.key.value); // theForm.plaintext.value = byteArrayToHex(rijndaelDecrypt(ct, key, "ECB")); } </script> </head> [syntax=php] <?php mysql_connect("localhost","root",""); mysql_select_db("encryption") or die(mysql_error()); $userId = $_POST['userId']; if (($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['key'] == "")) { $query = mysql_query("select * from employee_details where id = '$userId'"); if($row=mysql_fetch_assoc($query)) { echo '<tr>'; foreach($row as $value) echo '<td>'.$value.'</td>'; echo '</tr>'; } else { echo "No rows returned"; }} else if (($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['key'])) { $columname = "ciphertext"; $tablename = "employee_details"; function getField($field, $tbl_name, $condition) { $result = mysql_query("SELECT $field FROM $tbl_name WHERE id = ".$condition); return @mysql_result($result, 0); } $myValue = getField($columname,$tablename,$userId); echo "$myValue"; //doDecryption(); } echo '<script type="text/javascript"> doDecryption(); </script>'; echo "whats happening"; ?> </body> </html> [/syntax] <body> CODE THAT IS SENT AS HTML OUTPUT TO THE BROWSWER Code: [Select] <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>AES (Rijndael) Encryption Test in JavaScript</title> <script src="aes-enc.js" type="text/javascript" language="JavaScript"></script> <script src="aes-dec.js" type="text/javascript" language="JavaScript"></script> <script src="aes-test.js" type="text/javascript" language="JavaScript"></script> <script type="text/javascript" language="JavaScript"> function doDecryption() { document.alert("Inside Javascript"); var ct, key; var theForm = document.forms[0]; blockSizeInBits=128; keySizeInBits = theForm.keySize[theForm.keySize.selectedIndex].value; if (theForm.key.value.toLowerCase().indexOf("0x") == 0) theForm.key.value = theForm.key.value.substring(2); if (theForm.ciphertext.value.toLowerCase().indexOf("0x") == 0) theForm.ciphertext.value = theForm.ciphertext.value.substring(2); if (theForm.key.value.length*4 != keySizeInBits) { alert("For a " + keySizeInBits + " bit key, the hex string needs to be " + (keySizeInBits / 4) + " hex characters long."); if (theForm.key.select) theForm.key.select(); return; } if (theForm.ciphertext.value.length*4 != blockSizeInBits) { alert("For a " + blockSizeInBits + " bit block, the hex ciphertext string needs to be " + (blockSizeInBits / 4) + " hex characters long."); if (theForm.ciphertext.select) theForm.ciphertext.select(); return; } ct = hex2s(<br /> <b>Notice</b>: Undefined variable: myValue in <b>C:\wamp\www\AES4U\Decryptedresults.php</b> on line <b>41</b><br /> ); document.write("Inside Javascript"); document.write(ct); // key = hex2s(theForm.key.value); // theForm.plaintext.value = byteArrayToHex(rijndaelDecrypt(ct, key, "ECB")); } </script> </head> <body> <br /> <b>Notice</b>: Undefined index: userId in <b>C:\wamp\www\AES4U\Decryptedresults.php</b> on line <b>58</b><br /> // Do not worry about this notice because teh value has not been posted it is not able find this value. </body> This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=316624.0 I'd like to do a check if JavaScript is disabled in the user's browser, and based on that I'd like to do a if statement. How can I do that? I appreciate any suggestions. Thanks. I wasn't sure where to post this so I apologize if this is the wrong forum. I have a php page with over 50 numerical inputs on a form. A lot of the inputs are used to get data out of the mysql database so I want to be safe from sql injection. Is it safe to use a javascript onsubmit function that will not allow the form to be submitted if there is any non numeric data entered by the user? I've already written the function & it works fine but I'm not sure how much protection it gives me. Any feedback welcome. Hello, I am having some issues with a project I am working on. I am calling a php file on another server using Code: [Select] <script type="text/javascript" src="http://www.website.com/test/file.php"></script> I have the correct type header in my php file and the php outputs JS. example Code: [Select] echo "document.write(\"some stuff\");"; that kind of thing happens alot I also have it outputting a few JS functions, example Code: [Select] echo 'function dostuff(){ stuff }'; the problem I am having is when I add the JS call to the page that calls the php it does nothing I am using document.ready I am not really sure where to go from here, I know php well enough to do anything I need but I do not know JS at all I can not post the actual source code, I know that would be much easier to troubleshoot this but this is something I just can not do. Also this needs to be done this way, I need to call to php files on another server so they can do some stuff and then use JS to change some things on another page on another server. This is a content locker, a lot like CPALead and things like that. What am I missing? Any help is greatly appreciated How can i call a specific row when ever a like. example : Code: [Select] $record = mysql_query("select * from table_name"); ($getValue= mysql_fetch_row($record) //i'm not sure what should i use. should i use mysql_fetch_array, mysql_ fetch_ assoc, mysql_ fetch_ object, mysql_ field_ name and etc. print $getValue[$column_name][0]; 0 = value 1st row in the database/table_name 1 = value 2nd row in the database/table_name 2 = value 3rd row in the database/table_name ...... .... ...... ..... etc. is there a code i can do except from using sql_fetch_array then loop and put in into a multidimensional array? i have a database which contains a table named girls with three columns ...Serial(int) name(text) hits(int) <?php error_reporting(E_ALL ^ E_NOTICE); $con = mysql_connect("localhost","gaurav",""); mysql_selectdb("website",$con); $index1 = rand(1,$count); $sql = mysql_query("SELECT * FROM girls WHERE Serial=$index1"); $row = mysql_fetch_array($sql); $name1 = $row['name']; $hits1 = $row['hits']; echo <<<ECO <html> <script language="javascript"> increment(){ // some php code to increase the hits in database where name = $name1 } </script> <input type="button" value="increment" onclick="increment();" name="button"> </html> ECO; ?> sorry for the code to be really haphazard..i jst started with php.....now i dont know wat code to put to increment the hits in increment function since it would also use varialble $name1 and $index1..... I have a PHP config file called config.php. inside the file is simular to this: Quote <?php class JConfig { var $offline = '0'; var $editor = 'tinymce'; var $list_limit = '20'; } ?> From another file, i have included config.php, but how do I call $editor to get "tinymce"? Thanks How can I make a link that posts to a script? I using just a link but since it doesn't post I just get a URL with a variable and I can't do anything with it. If you need anything else then just ask I am using simple machines forum, and by placing a link to ssi.php in the top of my php page Code: [Select] <?php require_once('/forum/SSI.php');?>I am able to use some ssi functions. My problem is calling another function from within a function. My first function is this: Code: [Select] <?php $allowed_groups = array(1,5); $can_see = FALSE; foreach ($allowed_groups as $allowed) if (in_array($allowed, $user_info['groups'])) { $can_see = TRUE; break; } if ($can_see) { echo ' <div> this is text that i want certain groups to see </div>'; } else { //message or section to show if they are not allowed. echo 'this is text that i dont want certain groups to see'; } ?> This is my second function: Code: [Select] <?php ssi_welcome(); ?> I want to put the second function inside the first function, like so: Code: [Select] if ($can_see) { echo ' <div> <?php ssi_welcome(); ?> </div>'; this just gives me the "<?php ssi_welcome(); ?>" displayed on the actual site, it is not calling the separate function. I have tried different approaches, e.g. $function ssi_welcome; but all it does is display this as text as well.. Please can some one point me in the right direction. Thank you i have a constructor Quote public function __construct(){ // check if any action related to this page was generated if(isset($_GET['action'])){ $this->action = ucwords($_GET['action']) . '()'; self::{$this->maction}; } //code to generate the list of all the systems users $user = new userDAO; $this->results = $user->fetchAll(); } if action is present in the querystring, eg. delete, it would be stored into $this->action as Delete(). Similarly, if the action was edit, it would be stored as Edit(). My intent is to call the method named Delete() or Edit() depending upon which action was generated. I have defined the methods in the same class. Once i assign the current action to the $this->action, i want to call the method without explicitly specifying the method name.... here i have tried self::$this->action.... i even tried $this->action only but its not working? i think the string stored in the self::$this->action is not being interpolated? or....sth. i dont know. pls help out guys This is probably a quick one. I am pretty new to OOP, in fact somewhat a novice. I am working a lot more with objects lately as my previous "flat" php experience doesn't allow me to create clean and expandable applications. Of course though, questions will always pop up (that's what you guys are for?) One concept I am having trouble understanding, is the method to call a class. What is the difference between?: Code: [Select] <?php $cat = new class(); ?> and just simply: Code: [Select] <?php new class(); ?> Both appear to have the same output from my small amount of practice, but the first way seems strange to me. Being that I am very new to OO PHP, the first way looks like it is just defining a var. But in reality it is calling the class and running it? This seems very backwards to me and I am having a hell of a time understanding it. I am aware that the first way seems to be the "proper" way, but just can't fathom it. Could someone explain this to me a little further? thanks much, Hi, I got a little problem with calling a PHP function from a href. I got my site all set up, and am calling the function like this: Code: [Select] <?php echo '<a href="'.changeRecentPosts().'" class="image">' ?> This does indeed work, but it calls the function every time I refresh my page.. Which isn't the effect I was trying to achieve. I tried to change it to : Code: [Select] <?php echo '<a href="#" onClick="'.changeRecentPosts().'" class="image">' ?> But that didn't work either. It also seems it only calls it when I refresh my page, but never when I click it (I guess because PHP is handeled before HTML or something similar?). If you can think of any way (even in AJAX, or whatever other language...) feel free to, I think I can figure it all out. Thank you! Does anyone know if it's possible to call a class with an arbitrary number of variables? I'll explain what I mean. Here is my class construct definition: Code: [Select] function __construct($file, $outFile = 'newpdf.pdf', $fontSize = 70, $alpha=0.6, $overlayMsg = 'N O T F O R S H O P', $degrees = 45) { I know that I have to pass over the $file variable for it to work now. But can I create the class passing just the $file var and the $overlayMsg var? Everything else I would want to leave as default. How would I do that? Thanks Mike Hi, I was looking around the web for a image resizer script and I came across this one. http://www.scriptol.com/scripts/thumbnail-maker.php The weird thing is, it says to use this syntax to get it to work php resizer.php -j myimage.png Well.. I don't seem to understand how that makes it work, Normally I would run a php script by looking for it's main function and running it like: mainfunction(); Maybe I'm interpreting it wrong, but how is this script suppose to run? Hi, i'm new to php and am trying to call $variables in different in multiple hierarchy from one main variable file. I have one main file called variables.php that contain a list of variables <?php $variable1 = 'root/main/'; $variable2 = 'welcome'; $variable3 = 'testing'; ?> a second file called file1.php in the same directory as the variables.php file <?php require 'varibles.php'; include $variable1 . 'testing.php' ?> a third file called testing.php inside root/main/ <?php echo $variable2; include $varible1 . 'temp/abc.php'; ?> a fourth file called abc.php inside root/main/temp <?php echo $variable3; ?> the problem is testing.php is able to echo $variable2 but abc.php is not able to echo $variable3...is there a limit to the level of files before it can't echo a variable? |