Web Development Questions That Don't Need Their Own Thread v2
3,079 replies, posted
[QUOTE=zzlawlzz;33957367]i thought variables have to be defined. :/[/QUOTE]
forgot to mention, I've defined it earlier
[QUOTE=Alcapwne;33957282]There's something wrong:
[php]
$.each(json.results,function(i,tweet){
post = tweet.text;
$(post).filter();
etc
[/php]
filter() is a custom function I made which seems to be working. Something's not right though, only 1 result is being returned, as opposed to cycling through all the results in the json.[/QUOTE]
Are you returning false at any point within the callback function?
[QUOTE=deadeye536;33958338]Are you returning false at any point within the callback function?[/QUOTE]
the callback function is setting a variable as 'false' in order to determine whether an action is carried out in the 'etc' bit
[QUOTE=Alcapwne;33958345]the callback function is setting a variable as 'false' in order to determine whether an action is carried out in the 'etc' bit[/QUOTE]
[code]var results = json.results;
$.each(results,function(i,tweet){
post = tweet.text;
$(post).filter();
etc
[/code]
Try that.
Edit:
Someone in the comments on the page was saying they were having problems with nested arrays/objects.
[QUOTE=deadeye536;33958448][code]var results = json.results;
$.each(results,function(i,tweet){
post = tweet.text;
$(post).filter();
etc
[/code]
Try that.[/QUOTE]
still no luck :(
thanks for trying though, any other ideas?
I remade my theme selector a bit:
HTML:
[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title>Test</title>
<link href="styles/themes/<?php include("tema.php"); ?>/main.css" rel="stylesheet" />
<link href="styles/themes/dimensions.css" rel="stylesheet" />
<script src="new.js">
</script>
<script src="jquery.js">
</script>
</head>
[/code]
and PHP:
[code]<?php
$themeSelection = $_POST['themeChoice'];
if($_COOKIE['visitedalready'] != "yes"){
setcookie( "visitedlaready", "yes", time()+3600);
setcookie( "selectedtheme", "blue", time()+3600 );
}
else{
if(isset($_POST['themeSubmission'])){
if( $themeSelection == "blue"){
setcookie( "selectedtheme", "", time() -3600 );
setcookie( "selectedtheme", "blue", time() +3600);
}
else if( $themeSelection == 'red'){
setcookie( "selectedtheme", "", time() -3600 );
setcookie( "selectedtheme", "red", time() +3600);
}
else if( $themeSelection == "green"){
setcookie( "selectedtheme", "", time() -3600 );
setcookie( "selectedtheme", "green", time() +3600);
}
else if( $themeSelection == "orange"){
setcookie( "selectedtheme", "", time() -3600 );
setcookie( "selectedtheme", "orange", time() +3600);
}
else if( $themeSelection == "turtoise"){
setcookie( "selectedtheme", "", time() -3600 );
setcookie( "selectedtheme", "turtoise", time() +3600);
}
else if( $themeSelection == "yellow"){
setcookie( "selectedtheme", "", time() -3600 );
setcookie( "selectedtheme", "yellow", time() +3600);
}
else if( $themeSelection == "pink"){
setcookie( "selectedtheme", "", time() -3600 );
setcookie( "selectedtheme", "pink", time() +3600);
}
else if( $themeSelection == "mono"){
setcookie( "selectedtheme", "", time() -3600 );
setcookie( "selectedtheme", "mono", time() +3600);
}
else if( $themeSelection == "mono_real"){
setcookie( "selectedtheme", "", time() -3600 );
setcookie( "selectedtheme", "mono_real", time() +3600);
}
}
}
echo ($_COOKIE['selectedtheme']);
?>[/code]
Basically I want it to: if user enters page and has no cookie "visitedalready" to set it to yes and set cookie named "selectedtheme" to theme blue.
If user chooses other theme from drop down box then remove old cookie and put new one in with new cololor value.
Then that value is printed into head of HTML file specifying theme path basically.
Here is dropdown menu:
[code]<div id="themeSelector">
<table>
<form action="" method="post">
<tr><td>Select Theme:</td><td><select id="themeChoice" class="themes">
<option value="blue" select>Blue (Default)</option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="orange">Orange</option>
<option value="turtoise">Turquoise</option>
<option value="yellow">Dark Yellow</option>
<option value="pink">Pink</option>
<option value="mono">Mono (black)</option>
<option value="mono_real">Mono with icons</option>
</select></td><input type="submit" name="themeSubmission" value="Change"></tr>
</form>
</table>
</div>[/code]
But it wont work, also when user enters for first time, all page is blank only when refreshed it sets to blue. Help?
Does anyone know of an API that can retrieve information from an IP? Like country, city, ect.
[QUOTE=Floatation;33959563]Does anyone know of an API that can retrieve information from an IP? Like country, city, ect.[/QUOTE]
[url]http://php.net/manual/en/book.geoip.php[/url]
[QUOTE=arleitiss;33958964]...
But it wont work, also when user enters for first time, all page is blank only when refreshed it sets to blue. Help?[/QUOTE]
Because you're echoing the value of a cookie before it's set (When you set the cookie, that just means it's emitted to the output stream, you can't query the value until the client sends another query back)
That also means that clearing the cookie then resetting it on the next line won't have any effect, so you can just overwrite them without issue.
[QUOTE=Dragory;33956436]Alright, nevermind that, there's no point arguing about it. Which framework would you recommend and why? I was looking at CodeIgniter, but there seem to be very many alternatives.[/QUOTE]
Rails is nice
[QUOTE=TheDecryptor;33959924]Because you're echoing the value of a cookie before it's set (When you set the cookie, that just means it's emitted to the output stream, you can't query the value until the client sends another query back)
That also means that clearing the cookie then resetting it on the next line won't have any effect, so you can just overwrite them without issue.[/QUOTE]
So how exactly could I make that echo after cookie is set?
Use a proper variable, and set the cookie based on that.
[code]if (isset($_POST['themeChoice'])) {
switch (strtolower(trim($_POST['themeChoice']))) {
case "red":
$theme = "red";
break;
case "green":
$theme = "green";
break;
case "orange":
$theme = "orange";
break;
// etc.
default:
$theme = "blue";
}
setcookie( "selectedtheme", $theme, time() +3600);
echo $theme;
}[/code]
That should have the same effect, and looks cleaner.
[QUOTE=Dragory;33959759][url]http://php.net/manual/en/book.geoip.php[/url][/QUOTE]
It seems that lithiumhosting does not have it installed. Is there anything else I can try?
[QUOTE=swift and shift;33960140]Rails is nice[/QUOTE]
Django is nice too.
[QUOTE=Floatation;33961814]It seems that lithiumhosting does not have it installed. Is there anything else I can try?[/QUOTE]
[url]http://geolite.maxmind.com/download/geoip/api/php/[/url]
[QUOTE=Skorpy;33961922]Django is nice too.[/QUOTE]
lol
[QUOTE=swift and shift;33962345]lol[/QUOTE]
Opinions, opinions. Lovely opinions.
Hi, I'm having a small problem with jQuery/AJAX, and I wanted to post it here instead of making my own thread as it's probably a minor issue:
Essentially, I have two pages. Index.php and backend.php; index.php serves as the front page, backend.php does backend processing. Two pieces of data are sent to backend.php via POST:
+ url: This is collected from the user by means of a form on index.php
+ filename: This is a randomly generated filename; it is passed to backend.php which creates this file. The file is an XML file used by the backend code to record its activities. This XML file is then to be read by index.php to allow the activities of the backend code to be reported to the user.
The important thing is that backend.php is designed to be run for a very long period of time, it will not load and give a response unless something goes wrong. It simply carries on its function for a long period, recording its activities to the XML file, which is then read by the index.php in order to show the user what the backend code is doing.
I have working code that allows me to send the data via HTTP POST to the backend code, however the problem is that the code then waits for a response from backend.php, instead of moving on to the next section of code, which is designed to read the XML file. The code for index.php is:
[PHP]<html>
<head>
<title>Blah!</title>
<?php
//Generate XML filename:
function genfilename()
{
$name = substr(base64_encode(rand(1000000000,9999999999)),0,10);
return($name);
}
a:
$filename = genfilename().".xml";
if(file_exists("./logs/".$filename)) goto a;
?>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
//Use jQuery to send data via POST to backend.php
$(document).ready(function(){
$("#submitbutton").click(function(){
var url=$("#url").val();
var file="<?php echo $filename; ?>";
$.post("backend.php", {url:url , filename:file}); //The code stops here, waiting for a response from backend.php that won't come
// Code to read XML file created by backend.php (currently incomplete):
// I need this to execute after the POST has been sent
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//Print YEA! if it works
$("#events").html("YEA!");
}
}
xmlhttp.open("GET","<?php echo "./logs/".$filename; ?>",true);
xmlhttp.send();
});
});
</script>
</head>
<body>
<h1>Welcome to blah</h1>
<p>Please complete the form below: </p>
<table>
<tr>
<td>URL:</td>
<td><input type="text" id="url" /></td>
<td><button id="submitbutton" type="button">Submit</button></td>
</tr>
</table>
<br/><br/><br/>
<span id="events"></span>
</body>
</html>[/PHP]
So what I need to know how to do is read the XML file that will have been created by backend.php; the XML file will be constantly added to as backend.php goes about its activities.
Rob
[QUOTE=DrSwitched;33963969]
[PHP]
<?php
//Generate XML filename:
function genfilename()
{
$name = substr(base64_encode(rand(1000000000,9999999999)),0,10);
return($name);
}
a:
$filename = genfilename().".xml";
if(file_exists("./logs/".$filename)) goto a;
?>[/PHP][/QUOTE]
[PHP]
<?php
//Generate XML filename:
function genfilename()
{
$name = substr(base64_encode(rand(1000000000,9999999999)),0,10);
return($name);
}
do {
$filename = genfilename().".xml";
} while(file_exists("./logs/".$filename));
?>[/PHP]
[QUOTE=TheDecryptor;33960506]Use a proper variable, and set the cookie based on that.
...
That should have the same effect, and looks cleaner.[/QUOTE]
No, this is wrong. You should add "$theme = "blue";" before the if () statement, and move the echo out of it so it actually works.
Thanks for the suggestion, that's much neater than the silly goto method I was using xD. Any ideas as to how I got about solving the problem I mentioned? I'm a bit stumped
[code]
CREATE TABLE IF NOT EXISTS `deelname`(
`d_id` int NOT NULL AUTO_INCREMENT,
`s_id` int NOT NULL,
`to_id` int NOT NULL,
`inleg_geld_betaald` char(1),
PRIMARY KEY (`d_id`),
FOREIGN KEY (`s_id`) REFERENCES `speler` (`s_id`)
ON DELETE NO ACTION ON UPDATE NO ACTION,
FOREIGN KEY (`to_id`) REFERENCES `toernooi` (`to_id`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE = InnoDB;
[/code]
Could someone please tell me what I'm doing wrong? I'm getting a 1005 error, faulty SQL for my foreign keys.
Fuck me, just realized what I'm doing wrong..
Or not..
[QUOTE=Alcapwne;33957282]There's something wrong:
[php]
$.each(json.results,function(i,tweet){
post = tweet.text;
$(post).filter();
etc
[/php]
filter() is a custom function I made which seems to be working. Something's not right though, only 1 result is being returned, as opposed to cycling through all the results in the json.[/QUOTE]
Use a for?
[php]
for(var i = json.results - 1; i <= 0; i--){
post = tweet[i].text;
$(post).filter();
etc
[/php]
Can standart PHP file creation function be used to create new .php / .htm page? or only .txt? if only .txt please any suggestions how to create new .php/.htm file from php?
[QUOTE=arleitiss;33990901]Can standart PHP file creation function be used to create new .php / .htm page? or only .txt? if only .txt please any suggestions how to create new .php/.htm file from php?[/QUOTE]
What?
[QUOTE=kragmars102;33991514][code]
$fp = fopen("yourfile.php", "w");
$string = "your content";
fwrite($fp, $string);
fclose($fp);
[/code][/QUOTE]
Don't do this. Instead [url=http://php.net/manual/en/function.file-get-contents.php]file_get_contents()[/url] and [url=http://php.net/manual/en/function.file-put-contents.php]file_put_contents()[/url]. I hate it when people give complex ways to do easy shit to beginners.
He has to create the file first though.
[QUOTE=kragmars102;33992492]He has to create the file first though.[/QUOTE]
You might want to look into how those functions work.
[editline]1st January 2012[/editline]
If you can't read between the lines it creates it if it doesn't already exist.
Sorry, you need to Log In to post a reply to this thread.