Solved

Javascript / JQuery / PHP Query

Posted on 2011-02-23
13
512 Views
Last Modified: 2012-06-21
Hi all,

I need some direct, simple sample code to do the following.

Pages would be:

1. index.html
2. index.php

The index.html uses Jquery / Ajax to call the php and thr index.php queries the database and my index.html then displays the results.

Basically, I just need to php file to do the connection / query and the html / javascript page would do the rest?

The Database is MySQL by the way ...

Can anyone help please?

I just need the pure working basics for I can userstand it.

Thanks

Any suggestions
0
Comment
Question by:error77
  • 5
  • 5
  • 3
13 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34963655
Wouldn't it be easier to use only the php files ? Why additional html ?
Could you be more specific about the queries or the files, the code ? Do you have any ?
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34963665
You can put everything in one php file, jQuery, JS, PHP - in my opinion it would be better to debug.
0
 
LVL 5

Expert Comment

by:onemadeye
ID: 34963721
If you want the page to be accessible from browser as HTML file then you can do that later via HTACCESS ... and for now, as @Roads_Roads said, I think it will be better to work it on 1 PHP file.
0
 

Author Comment

by:error77
ID: 34963819
It's for use on a mobile so basically I can only use HTML, Javascript and CSS ... So ehat I thought was to query a online php file or something ... unless have a better suggestion? All the data is on an online database and I need to get it but cannot use php only html and javascript on the mobile.
0
 

Author Comment

by:error77
ID: 34966106
Anyone please?
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34967666
So the html page would have some kind of form right ?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:error77
ID: 34967840
Yes
0
 
LVL 5

Expert Comment

by:onemadeye
ID: 34968117
If you have it written in a PHP file and able to access it from regular browser as
http://yoursite.com/yourfile.php

Next, add a .htaccess to the root and fill this:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L]

Open in new window


And then the same file above is accessible through
http://yoursite.com/yourfile.html

Try it...
0
 

Author Comment

by:error77
ID: 34968434
Sorry, onemadeye, I don't think you understood my question. I'm not looking into htaccess issues, I need to connect to an sql database using html / ajax into a php ... html / ajax being local ... and a request being made to the php file which is remote.
0
 
LVL 5

Expert Comment

by:onemadeye
ID: 34968485
Nope, I think you dont understand the solution that i gave...
You're looking for 50+50=100 and I come up with 25+75=100 and you said I'm wrong?

But ok, let just do this your way, as said you need to connect to an sql database using html / ajax into a php ... html / ajax being local ... and a request being made to the php file which is remote.
And how do you want us to grant you a solution??
What have you achieve so far? How bout let us know your codes so far?
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 34968942
No need to be irritated. This can be done with htaccess and only php files. But that would be assumption that te=here is an apache server instead of iis (for example). Let's stick to the html + ajax -> php way.

This would be the html file

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.5.js"></script>
</head>
<body>
<script>

    function checkForm()
    {
            $.ajax({
                type: "POST",
                url: "./check.php",
                data: $('#myform').serialize(),
                dataType: "json",
                success: function(msgadd){
                
                    alert(msgadd.txt);
             
                }
            });
    }

    $(document).ready(function(){
        $('#myform').submit(function(e){
            checkForm();
            e.preventDefault();
        });
    });

</script>


<form action="#" method="post" id="myform">
<input type="text" name="something" />
<input type="submit" value="send" />
</form>

</body>
</html>

Open in new window

0
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 500 total points
ID: 34968959
And this would be the check.php file

<?php header('Content-Type: text/html; charset=utf-8'); ?>
<?php
// CONNECT TO DATABASE
//include("dbconnect.php");

function msgadd($errorno,$txt)
{
	return '{"errorno":'.$errorno.',"txt":"'.$txt.'"}';
}	

if(empty($_POST[something]))
{
    die(msgadd(1,"Empty something"));
}	
else
{

    // HERE A DATABASE STUFF, CHECKING ETC
    die(msgadd(1,"Done."));

}	

?>

Open in new window

0
 

Author Closing Comment

by:error77
ID: 34969312
Thanks!

p.s: onemadeye, didn't mean't for you to get offended. I'm sure your solution is fine and works but it wasn't what I was looking for. I was looking for HTML and PHP code. Thanks again.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now