开发者

Accessing MySQL through Javascript via PHP

开发者 https://www.devze.com 2023-03-01 06:16 出处:网络
When any user logs in, I just want to send 2 parameters to PHP and then want to update related DB table. Is it possible in javascript? If possible any sample c开发者_高级运维ode or links appreciated.Y

When any user logs in, I just want to send 2 parameters to PHP and then want to update related DB table. Is it possible in javascript? If possible any sample c开发者_高级运维ode or links appreciated.


You can use JavaScript to request a PHP script using XMLHttpRequest, and include some database interaction with MySQL in the PHP script.


AJAX is the key you're looking for.

A javascript framework such jQuery could help you a lot. There's several built in AJAX methods related in the documentation, specially this method.


It is really pretty simple using JQuery, as pedrorezende said. The easiest way would be something like

$.post('sample/path?var1=value&var2=otherValue');

Edit: here is the full code, I believe this would accomplish what you want (untested):

<script type="tex/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"> 
</script>
<script type="text/javascript">
  $("#login_form").submit(function() {
     $.post("login_handler.php", $("#login_form").serialize());
  }
</script>
<body>
    <form id="login_form">
        <input type="text" name="username"></input>
        <input type="text" name="password"></input>
        <input type="submit" value="login">
    </form>
</body>

The first script will load JQuery.

The second script creates the handler and sends the form data to an external PHP file.

Then in login_handler.php you would send your query to MySQL using the $_POST values.

0

精彩评论

暂无评论...
验证码 换一张
取 消