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 code 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.
Related
My question is should I convert two html pages to php pages, so the called page can access its POSTed parameters, or is there a way for a called html (.html extension) page to access posted parameters?
I've been reading that because posted parameters are server-side there is no way for JavaScript to do this being client side. I've seen nothing about one html page accessing
parameters if that .html page was accessed via a POST.
Here is my calling form. The called form, needs access to TransDesc (below), which is a text field.
<script type="text/javascript" language="JavaScript1.2">
// Check where we came from so that we can go to the right spot when the
// form gets posted from outside of our HTML tree.
var PostURL = '/event.html';
</script>
Enter a Donation Amount
<script type="text/javascript" language="JavaScript1.2">
document.write(
'<form name="InvGenPayDonation"
action="'+PostURL+'"
onsubmit="return validateForm();"
method=POST>');
</script>
<p> $ <input type='text' name='Amount' value="0.00">
</p>
<p>In honor of <span style="color: #FF0000">
<input type='text' name='TransDesc' id='TransDesc' value="" >
</p>
<input type="submit" value="Next"> <br /><br />
A static HTML file cannot access variables that have been POST'ed to it. It can't even know they're there as they're sent to the server in the HTTP request, the server then deals with them and sends the HTML page in the HTTP response. They're 'consumed' before the page is even sent to the client.
You could use GET and access them via JavaScript, or configure Apache to server .html files as PHP files instead though.
In my opinion, php is the easiest way to go, and as far as languages go is pretty easy to learn and pretty intuitive.
You'll have to either convert them to PHP or use GET instead of POST, as GET parameters are accessible through window.location.href
Yes, I would recommend converting the pages to php. If you are set on using HTML files you will have to edit your htaccess file to run HTML pages as php.
You can always use ajax to retrieve and send post and get values.
You can retrieve it with js by creating a php file and access those with ajax from your html files.
I've never met such approach before, but I'm wondering why nothing happens after clicking the "Say hello" button.
<html>
<head>
</head>
<body>
<button onclick="myscript.php">Say hello</button>
</body>
</html>
<?php
echo "Hello";
?>
It would look very "bombastic" if I used the "form" markup construction in this case.
Could you point out my mistake? Or if my vision is totally hopeless, what are the unconventional ways to run the php script?
Why are you using onclick? onclick executes javascript code.
Just link to the file
Say Hello
If you must use javascript, you have to redirect to that page using something like window.location
Here's a tutorial on redirecting with javascript
Because onclick events evaluate JScript or JavaScript code, not file name.
http://www.w3schools.com/jsref/event_onclick.asp
onclick is a so called Event-Property. That expects javascript code by default.
this example is equivalent to including a js file with the content
test.php
that would raise an error as there is no object called test... You could put it in quotes which would make it a string and solve the problem... at least no error would be raised.
But back to your question: You don't launch a php script do you? with
<form method=GET action="scriptname.php">
<input type="text" name="name" />
<input type="submit" value="submit" />
</form>
you tell your browser to open a php-file with additional informations from the form.
It is of course possible to communicate with a php file in the background, search for ajax in order to do that.
I have a form that when the person selects the date I want the value to go into a php function.
I can view the value by using this.form.orderdate.value, but I want to put that value into the php variable $dateChosen
Here is the page so far:
TDM KML Generator
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<script type="text/javascript">DateInput('orderdate', true, 'YYMMDD')</script>
<input name="submit" type="submit" value="get KML" />
</form>
</body>
Javascript is all executing on the client machine, with the exception of an ajax call (which is still browser/client side and is making a request to a server-side function). PHP happens all server-side. In your case, you're trying to do something that isn't possible, because the PHP script is no longer executing by the time the page is rendered in the browser and the javascript is able to execute.
I want to create a login script on HTML page. I read that I need to use an intermediate script so that I can let my HTML use the database. So, I`ve written a PHP page that checks the username/password regarding the database.
I also read that I should use AJAX to connect to the database.
I would like to know how to write the AJAX code to return the value of the login either true or false.
You should really at-least try Google before posting here.
Google Search: ajax login code
This is a very simple implementation but can be a starting point.
http://woork.blogspot.com/2007/10/login-using-ajax-and-php.html
You may need to read up on ajax a little if this doesn't make sence:
http://code.google.com/edu/ajax/tutorials/ajax-tutorial.html
AJAX is really nothing more than JavaScript which connects to a server-side resource (such as a PHP-backed page), receives a result, and likely does some UI manipulation of the HTML as a response to that result. A good place to get started is the jQuery ajax method. Using the jQuery JavaScript library will make the process much simpler. But, ultimately, it'll connect to your PHP code on the server to perform the actual database interaction.
Beyond that, it sounds like you're lacking a good bit of design oversight in this project. The statement "I also read that I should use AJAX to connect to the database." is particularly troubling. Where did you read that? Why did you read that? There seems to be little value in that suggestion beyond someone somewhere thinking that "AJAX is cool and people should use it for stuff."
Is there a specific design concern for using AJAX vs. just posting a form to some PHP code?
Although it's not necessary to use AJAX for this, you can use something like below.
Considering you have an element that has a username and password like below:
<div id="login">
<label for="txtUsername">Username:</label>
<input type="text" id="txtUsername" />
<label for="txtPassword">Password:</label>
<input type="password" id="txtPassword" />
<button id="btnLogin">Log In</buton>
</div>
<div id="logout" style="display: none;">
Log Out
</div>
Then, having jQuery already referenced call your PHP page ("login.php" in the example):
<script type="text/javascript">
$(function() {
$("#btnLogin").click(function() {
$.ajax({
url: "login.php",
data: {username: $("#txtUsername").val(), password: $("#txtPassword").val()},
success: function(data){
$("#login").toggle();
$("#logout").toggle();
}
});
});
});
</script>
If I understand your question correctly, there are two ways you could handle this. The first (and most common) way is to just set the PHP script as the action of your HTML form:
<form method="post" action="folder/phpfile.php">
<input type="text" name="username" />
<input type="password" name="password" />
</form>
After doing this, make sure your PHP script takes in the post variables as $_POST['input_name'], where input_name is the name you used in the inputs on your HTML form (as 'username' and 'password' are used above).
Using AJAX for authentication is not always the best way to handle things in my opinion, but it is doable. I would recommend you try using JQuery behind your JavaScript if you are going to go the AJAX route. They have a really handy framework for handling AJAX queries and events.
I have an html form which uses a PHP file to submit data by email. I want to add some code (which I already have) to generate random numbers for spam protection. Can I call another PHP file within my form?
Here is the code that goes in the form:
<form name="mail" action="go.php" method="post" onsubmit="return CheckData()">
<input type="text" name="q">
<input type="submit" name="Submit" value="OK">
</form>
I am a real novice with PHP so any help would be appreciated.
LozFromOz
you can do it with image in your form that call to php file.
the famous is to use captcha,
read this link :
Stopping scripters from slamming your website hundreds of times a second
a good captcha to insert in php :
http://recaptcha.net/plugins/php/
There's no need to have the browser make two http requests for two different urls to the webserver. Your php script go.php can do what ever you want it to do, e.g. include two other scripts and/or calling two functions or ...
<?php // go.php
require_once 'spam_protection.php';
require_once 'form_helper.php';
require_once 'database_something.php';
....