Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Passing a result from Javascript into php to query database
Old 04-17-2012, 10:49 PM Passing a result from Javascript into php to query database
Junior Talker

Posts: 1
Trades: 0
Hi there,

I am currently making a widget through javascript.

The basic aim is when a user types in a particular web domain, (which i have preloaded in my database called - 'domain') and load the domain matching into the title bar of my widget. Then I have sorting buttons underneath, name, visits, and it basically just displays the number of times that site has been visited. So quite basic, but i am pretty new to all this and in desperate need of help from anyone.

Many thanks in advance!

the php will query the database to check for matching names of domains to what the user has typed in.

This is my php scirpt page called getDomainInfo.php -

<?php
ini_set("error_reporting", E_ALL);
ini_set("log_errors", "1");
ini_set("error_log", "./php_errors.txt");

require_once("db.php");

$userDomain = $_GET['domain'];


/* Construct a PHP array object containg the data for each section in the domain */
$info = array();


/* Add code here to query the DB for information about the given domain*/
$query = ("SELECT domain FROM sites WHERE domain LIKE '%$userDomain%'");
$select = mysql_query($query);

while ($row = mysql_fetch_array($select))
{
$info[] = $row[0];
}


/* Return a JSON encoded version of the array to the browser */

echo json_encode($info);

/* Close connection */
mysql_close();
?>

It uses another connections page which I have called db.php.

What im basically trying to query is . . . look for domain names that look like whatever the user has typed in . . . . from the main screen.

Last edited by wizniks; 04-17-2012 at 10:51 PM..
wizniks is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-26-2012, 07:29 AM Re: Passing a result from Javascript into php to query database
Junior Talker

Posts: 1
Trades: 0
if you want to do an Search for all the available domains in your database against the user typing the text,..
you can do it by more than one way..!
1. if you are familiar with html forms, then you can post the data entered by the user on the same page,. & then you can use your Php code to talk to mysql for the query. However, i will prefer you too use PHP pdo object or mysqli object whenever connecting to a database..!:

2) you can use Ajax, PHP and mysql together as if you don't want any kind of page loads.. as they work b/w any browser page requests..

I will prefer you to visit w3schools.com for more information.
Zahinalwa is offline
Reply With Quote
View Public Profile
 
Old 04-26-2012, 03:33 PM Re: Passing a result from Javascript into php to query database
miki86's Avatar
Extreme Talker

Posts: 239
Location: print_r($serbia);
Trades: 0
Your getDomainInfo.php page looks fine.
You can only change line:
PHP Code:
$userDomain $_GET['domain'];
// to
$userDomain mysql_real_escape_string($_GET['domain']); 
Always escapes special characters before running a query.

What exactly is the problem?

Im gonna take your thread title:

Quote:
Passing a result from Javascript into php to query database
You can't pass a result from javascript to a php page that is already loaded.
Php is what your server sees and load first, javascript comes after and is ran on users browser.

What you can do is make a new request using XHR (XmlHttpRequest or simply Ajax) and manipulate the result anyway you like (using javascript).

You have some good examples here:
http://www.w3schools.com/ajax/default.asp
miki86 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Passing a result from Javascript into php to query database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13988 seconds with 11 queries