Javascript / JQuery / PHP Query

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
error77Asked:
Who is Participating?
 
Lukasz ChmielewskiConnect With a Mentor Commented:
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
 
Lukasz ChmielewskiCommented:
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
 
Lukasz ChmielewskiCommented:
You can put everything in one php file, jQuery, JS, PHP - in my opinion it would be better to debug.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
onemadeyeCommented:
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
 
error77Author Commented:
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
 
error77Author Commented:
Anyone please?
0
 
Lukasz ChmielewskiCommented:
So the html page would have some kind of form right ?
0
 
error77Author Commented:
Yes
0
 
onemadeyeCommented:
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
 
error77Author Commented:
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
 
onemadeyeCommented:
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
 
Lukasz ChmielewskiConnect With a Mentor Commented:
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
 
error77Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.