I am building a mobile app using Titanium Studio, build: 3.1.1.201303242433, CLI version 3.0.24, Titanium SDK version 3.0.2.GA, iOS SDK: 6.1, iPad Simulator: 6.1 on MAC OS X10.7.5.
I am attempting to retrieve JSON data from a remote database. The database has records with and without a "sectionid" (sectionids are numbers 1-12). I want to retrieve the records with the "sectionid" I pass to my query. Here are the steps I am following: First I assign a "sectionid" to a tableViewRow like this:
leftImage:'images/advertising.png',
title:'Advertising',
sectionid: '1',
color:'#000',
font: {fontWeight:'bold', fontSize:16},
height:'44dp',
hasChild:true,
test:'testcatmenu.js'},
Then I pass the sectionid to another file like this:
var section = e.rowData.sectionid;
win.section = section;
In the next file I get the sectionid and pass it to the remote php file like this:
var section = Ti.UI.currentWindow.section;
var url = "http://mydomain.com/myfile.php?sectionid='section'";
Finally, in the php file I $_GET the sectionid and use it in my MySQL query like this:
$result = mysql_query("SELECT name FROM `cms_client` WHERE sectionid = '" . mysql_real_escape_string($_GET['section']) ."'") or die('Could not query');
The php file is returning the records that do not have a sectionid, so somewhere I am losing the sectionid? I do not know where my code syntax is wrong, would someone be so kind to show me the correct syntax that I need to make this work?
In the php you are using a variable $_GET['section'] but the GET variable you are passing in is called sectionid (?sectionid=). So either change both to section or both to sectionid.
Also, assuming that the
var url = "http://mydomain.com/myfile.php?sectionid='section'";
is javascript then it should be something like:
var url = "http://mydomain.com/myfile.php?sectionid="+section;
var url = "http://mydomain.com/myfile.php?sectionid='section'";
Change that to
var url = "http://mydomain.com/myfile.php?sectionid="+section;
Javascript allows you to add variables to a string using + variableName
Like
var variableT = "foobar";
var strNew = "This string contains the word: "+variableT+" and it works!";
Related
I'm trying to assign the return value of a php script to a js variable. I have this:
var email = jQuery("input[name=email]").val();
var emailRegex = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,})?$/;
var exists='<?php
$query = "SELECT * FROM rss_members WHERE email_id=\"something#email\"";
$results = mysql_query($query);
$results = mysql_num_rows($results);
echo $results;
?>';
console.log(exists)
The query works and I get the correct results back, but I want to replace "something#email.com" with the email variable, but if I write something like email_id=\"'+email+'\"..., the query result comes back incorrect. What am I doing wrong?
You aren't taking in consideration execution time.
When you make a request to a server (enter www.google.com for example) the server gets a request, and replies with a HTML page. The server in your case is in PHP and it sends a HTML page with some javascript included in it. After the browser receives the HTML page, it then interprets it, and run javascript code.
So basically, your php code ran without access to the email variable. If you wanted to have access to server-side information after the page loaded, you would need to make an ajax request
I have a domain using Php but I added asp.net code. And try to execute that it displayed asp.net code only. Whether it is possible to add asp.net code under php domain by using any plugin or some third party help. If yes means, give some idea.
You could use HttpWebRequest to get a result off a PHP page which might help you a bit. An example taken from: https://stackoverflow.com/a/9818700/4068558
string myRequest = "abc=1&pqr=2&lmn=3";
string myResponse="";
string myUrl = "Where you want to post data";
System.IO.StreamWriter myWriter = null;// it will open a http connection with provided url
System.Net.HttpWebRequest objRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(myUrl);//send data using objxmlhttp object
objRequest.Method = "GET";
objRequest.ContentLength = TranRequest.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";//to set content type
myWriter = new System.IO.StreamWriter(objRequest.GetRequestStream());
myWriter.Write(myRequest);//send data
myWriter.Close();//closed the myWriter object
System.Net.HttpWebResponse objResponse = (System.Net.HttpWebResponse)objRequest.GetResponse();//receive the responce from objxmlhttp object
using (System.IO.StreamReader sr = new System.IO.StreamReader(objResponse.GetResponseStream()))
{
myResponse= sr.ReadToEnd();
}
Otherwise, the problem is IIS will see a .php file and compile it with PHP. Vice versa with ASP. Although a work around for running PHP inside ASP.NET is phalanger.
i am new to Javascript and i have created the code below it works fine no problem at all however i want to know what is i want to pull the image dynamically using php and javascript from mysql database how can i refactor my code bellow. thanks in advance for your contribution.
var myimage = document.getelementById("mainImage");
var imageArray =["images/overlook.jpg","images/garden.jpg","images/park.jpg"];
var imageIndex =0;
function changeimage(){
myimage.setAttribute("src",imageArray[imageIndex]);
imageIndex++;
if(imageIndex >= imageArray.length){
imageIndex = 0;
}
setInterval(changeimage, 5000);
One of several options.
Query the database for the column with the URL of the images.
$query = mysql_query("SELECT url FROM images");
Then something like this to get an array out of it:
$images = array();
while($row = mysql_fetch_array($query)){
$images[] = $row['url'];
}
Then generate this string (that you use in the Javascript provided):
var imageArray = ["images/overlook.jpg","images/garden.jpg","images/park.
using the array you retrieved from the database. You could use json_encode in PHP for this if you don't want to mess around with error prone string building.
$imagesAsJsonArray = json_encode($images);
Echo it. Done.
Not the most elegant of solutions. But it gives you something to play with. Check out a few PHP tutorials online and you'll soon get the hang of it.
Two choices:
Using PHP when your page is created, put an array of images in the page and use page-level javascript to cycle among them.
Using Ajax in the page, call from the page to the server to get the next image and then use client-side javascript to make that returned image visible on the page.
Ive been trying to solve some issues for implementing google maps with the api using this guidehttp://code.google.com/intl/no-NO/apis/maps/articles/phpsqlajax_v3.html.
Im stuck at this part: modifying the query with a variable set in another document or in the same document with processing of server basename like this:
*(this is the php file that generates the xml file:http://gmaps-samples-v3.googlecode.com/svn/trunk/articles/phpsqlajax/phpsqlajax_genxml3.php)*
$mapid = str_replace(".php","",basename($_SERVER['REQUEST_URI']));
$query = "SELECT * FROM markers WHERE type='".$mapid."'";
This doesnt work...
Also it doesn't work if i set the variable in the main webpage.php file that displays the map eighter... it works if i set the variable in the same file (xmlgenerate.php) like this:
$mapid = "mymap";
$query = "SELECT * FROM markers WHERE type='".$mapid."'";
Hope someone can tell me what im doing wrong here.
edit: Heres a link to the live files btw. xmlgenerate=> http://www.pinnedmap.com/mapdata.php and mappage=> http://www.pinnedmap.com/test.php NB. a valid query example is: wondersoftheworld
to pass the variable from test.php would be ideal.
I don't understand what you're trying to do here:
$mapid = str_replace(".php","",basename($_SERVER['REQUEST_URI']));
Instead try this:
$mapid = mysql_real_escape_string($_GET['mapid']);
(The mysql_real_escape_string part prevents a SQL injection)
Now just request your file like file.php?mapid=something and everything should work...
A project of mine involves a flash movie (.swf) in a webpage, where the user has to pick from a number of items, and has the option to thumbs up or thumbs down (vote on) each item.
So far I have gotten this to work during each run of the application, as it is currently loading the data from an XML file - and the data is still static at the moment.
I need to persist these votes on the server using a database (mySQL), such that when the page is reloaded, the votes aren't forgotten.
Has anyone done this sort of thing before?
The two mains methods that I have found on the 'net are
either direct communication between AS3 and the SQL using some sort of framework, or
passing the SQL query to a PHP file, which then executes the SQL query and returns the SQL to AS3.
Which of these methods is the better option?
For the latter method (involving PHP), I have been able to find resources on how to acheive this when attempting to retrieve information from the database (i.e. a read operation), but not when attempting to send information to the database (i.e. a write operation, which is needed when the users vote). How is this done?
Thank you!
Edit: Implemented solution
Somewhere in the PHP file:
if ($action == "vote")
{
$id = $_POST['id'];
$upvotes = $_POST['upvotes'];
$query = "UPDATE `thetable` SET `upvotes` = '$upvotes' WHERE `thetable`.`id` = '$id' LIMIT 1 ;";
$result = mysql_query($query);
}
Somewhere in the ActionsScript:
public function writeToDb(action:String)
{
var loader:URLLoader = new URLLoader();
var postVars:URLVariables = new URLVariables();
var postReq:URLRequest = new URLRequest();
postVars.action = action;
postVars.id = id;
postVars.upvotes = upvotes;
postReq.url = <NAME_OF_PHP_FILE>;
postReq.method = URLRequestMethod.POST;
postReq.data = postVars;
loader.load(postReq);
loader.addEventListener(Event.COMPLETE, onWriteToDbComplete);
}
I am not aware of any framework that supports method-1.
I would use method-2 - but instead of making the query within Flash and passing it to PHP, I would rather pass the related data and construct the query in PHP itself. This is safer because it is less susceptible to SQL injection attacks.
This answer has an example of sending data from flash to the server - it talks about ASP, but the method is same for PHP (or any technology) - just change the URL.
Within the php code, you can read the sent data from the post $_POST (or $_GET) variable.
$something = $_POST["something"]
Many different options:
AMFPHP - binary messaging format between PHP and Actionscript/Flash.
LoadVars - for POSTing and GETing values to a PHP script.
JSON - Using the AS3Corelib you can post JSON formatted data to your web site (just like an AJAX script does).