JQuery.Validate() - remote rule - php

I'm developing a Web site for a school project. Though I mainly work with PHP, I thoght I should as well do some client-side validation.
It took a very short search in Google to find the Validate JQuery plugin.
And so far this is what I've got:
<form id="regform" class="well" method="post" action="Registering.php">
<label>Name: <input type="text" placeholder="Type your name" name="Name" class="required" /></label>
<label>Surname: <input type="text" placeholder="Type your surname" name="Surname" class="required" /></label>
<label>Username: <input type="text" placeholder="Type your username" name="Username" class="required" /></label>
<label>Password: <input type="password" placeholder="Type your password" name="Password" class="required" /></label>
<label>Repeat Password: <input type="password" placeholder="Confirm Password" name="Password-confirm" class="required" /></label>
<label>E-Mail: <input type="Email" placeholder="Type your Email" name="Email" class="required email" /></label>
<label>Birthday: <input type="Date" name="Birthday" /></label>
<label>Gender:</label>
<label>Male <input type="radio" name="Gender" value="0" checked> Female <input type="radio" name="Gender" value="1"></label>
<label>Photo: <input type="url" placeholder="Paste an image link" name="PhotoLink" class="url" /></label>
<label><input type="submit" class="btn btn-primary" name="Submit" value="Register in YouTune" /></label>
</form>
This is the form, pretty much a standard registration one. Nothing to add here.
And then I have this small Javascript:
$(document).ready(function(){
$("#regform").validate({
rules: {
Username: {
remote: { url: "/check_username.php", async: false }
}
},
messages: {
Username: { remote: "Username already on use. Pick a different one." }
}
});
});
Both scripts of the lastest JQuery library and the version 1.10 of the Validate Plugin were added on the head of the document.
Well, to make it short, the validation stuff runs smooth, except that the remote rule is not working. It is as if it wasn't there at all, no error message whether I submit with an already used Username or not. The php file that the Javascript is calling has the following script:
<?php error_reporting(E_ALL);
require_once("../Clases/User.php");
header('Content-type: application/json');
$vUser = new User();
$vResult = $vUser->GetUserByUsername($_REQUEST["Username"]);
if ($vResult) {
echo false;
} else { echo true; }
?>
The GetUserByUsername method goes to a DataBase and searches for a User with the given parameter, then returns true if there was a match up, false if there wasn't.
I've tested it without the remote call from the Javascript and it works fine
So, does anyone know why is this happening?
Thank you beforehand for taking the time read and trying to help me with this small issue.
EDIT: Solved. I just had to fix the source for the remote call. Can't believe I missed that. Anyway, thank you very much for the help.

I am not very sure but see if it works; remove "/" from your url. use "check_username.php" in place of "/check_username.php" or absolute url

Related

password attribute filling unwanted usernameand password

I'm trying to create a login page in php.
I got the form and the login verificaion works fine but when i go to my index.php which is my log in page, the login credentials are auto filled with unwanted credentials like below.
Whereas my login paage should look like this..
My form is as below:
<form action="check_login.php" method="post">
<label id="uname">User Name: </label><input type="text" placeholder="Enter Username" name="uname" required="required"> <br>
<label id="psw">Password: </label><input type="password" placeholder="Enter Password" name="psw" required="required"> <br> <br>
<button type="submit" name="login">Login </button>
</form>
When I use type=text it works fine..
I've also tried autocomplete="off", but it doesn't work...
What am I doing wrong? I'm a newbie... Please help me...
change
<input type="text" placeholder="Enter Username" name="uname" required="required">
with
<input type="text" placeholder="Enter Username" name="uname" required="required" autocomplete="off" />
it is browser feature to auto fill the form
"Autofilling" a form is a browser feature. You can turn it off for a form using the autocomplete attribute (setting it to off). Change your form to
<form action="check_login.php" method="post" autocomplete="off">
<label id="uname">User Name: </label><input type="text" placeholder="Enter Username" name="uname" required="required"> <br>
<label id="psw">Password: </label><input type="password" placeholder="Enter Password" name="psw" required="required"> <br> <br>
<button type="submit" name="login">Login </button>
</form>
There might be cases where autocomplete="off" is set, but the browser still fills in your form details. The "trick" is to set autocomplete to an invalid value, like autocomplete="kerbholz".
I got it...removed the user from saved logins from my browser.... And it works fine...

php $_POST problems

Ok, I know this is probably a really dumb question but I can't figure out what is going on here. I've got this in my original html file-
<form action="./api" method="POST">
<label for="first-name">First Name: </label>
<input id="first-name" type="text" name="first_name" /><br/>
<label for="last-name">Last Name: </label>
<input id="last-name" type="text" name="last_name" /><br/>
<input type="submit" name="send" value="Submit Form"/>
</form>
In my api folder I have an index.php file that has the following code-
<?php
print_r($_POST);
?>
Thats it- I know its not safe but I was just trying to get it to work. It returns an empty array...
Array ()
I really need a second pair of eyes on this, what am I missing? why isn't the information being transmitted?
Update:
changing the action to action="./api/index.php" returns the posted variables as expected. As someone commented below, I was hitting the index.php in the api folder before because it was returning the empty array. Its strange that it didn't accept the data. Is it common practice in php then to make the target of your post explicit?
Please add the full file path to your action and convert your
$_POST[''] to variables. The code below should work perfectly based on your question.
<form action="../api/index.php" method="POST">
<label for="first-name">First Name: </label>
<input id="first-name" type="text" name="first_name" /><br/>
<label for="last-name">Last Name: </label>
<input id="last-name" type="text" name="last_name" /><br/>
<input type="submit" name="send" value="Submit Form"/>
</form>
Add this to ../api/index.php
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
echo $first_name;
echo $last_name;
?>

How to store html form data in a cookie so it's still there after the form is submitted

I have made this html form, it submits all the information to a MySQL database. Pretty simple stuff.
How do I store the username in a cookie so that once it's entered? I would like to ensure that once the user enters their username once, it pre-populates the username field in the future.
<!-- Message board submissionform -->
<form id="frmMB" name="frmMB" action="insert.php" method="post" enctype="multipart/form-data">
<label class="name"><input name="name" placeholder="Enter your name" type="text" id="name" onFocus="if(this.value=='Enter your name'){this.value=''};" onBlur="if (this.value==''){this.value='Enter your name'};" value="Enter your name" size="80" maxlength="10" ></label>
<br />
<label class="message">
<input name="post" placeholder="Enter a message" type="text" id="post" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;" value="Enter a message" size="80" maxlength="140" data-maxsize="3">
</label>
<br />
<label>
<input name="Submit" class="large button" type="submit" value="submit">
</label>
I think something like this would just do this trick.
setcookie($_POST['name'], $value, time()+3600);
if(isset($_COOKIE['name']) && !empty($_COOKIE['name']))
{
// do some stuff here because the name is set in the cookie.
}
First you need to start a session -> session_start()
Once done so, you have created a public $_SESSION array and can add elements to it. Easiest way to do so is:
<?php
if(isset($_POST['username'])){
sesion_start();
$_SESSION['username'] = $_POST['username'];
}
...
Cheers!

wordpress login issue

i have created a custom popup login box. and use form settings like that
<form action="<?php bloginfo('url'); ?>/wp-login.php?redirect_to=<?php echo urlencode($_SERVER['REQUEST_URI']); ?>" method="post" autocomplete="off">
and i use input feilds like that
<input id="login_username" name="user_name" type="text" value="" class="pop-input surfix" /><span id="placeholder_user_login">Username</span>
<input id="login_password" name="user_password" type="password" value="" class="pop-input" /><span id="placeholder_user_pass">Password</span>
<input id="register" type="submit" value="Login" class="popup-login-btn" />
but it redirecting to wp-login.php except it should process the login and redirect to the pages where from user started login.
please guide me how can i fix this.
thank you all
actually the issue was with the name element of the input field.
When i check the wp-login.php page i discover the wordpress changes the name of input fields.
<input id="login_username" name="user_name"
<input id="login_password" name="user_password"
to
<input id="login_username" name="log"
<input id="login_password" name="pass"
So i just simply replace the names and all issues were sorted out.
Thank you all

HTML contact form in PHP block

I'm relatively new to PHP so forgive me if this seems obvious. How would you integrate a block of HTML code like this:
<form action="form.php" method="post" enctype="multipart/form-data">
<label></label>
<input name="name" required="required" placeholder="Your Name">
<label></label>
<input name="email" type="email" required="required" placeholder="Your Email">
<label></label>
<input name="address" type="name" required="required" placeholder="Your Address">
<label></label>
<textarea name="message" required > Dear Mr. X, Please support us... </textarea>
<input id="cancel" name="cancel" value="Cancel" />
<input id="submit" name="submit" type="submit" value="Submit">
</form>
into here:
if(empty($mpemail)){
echo "<h2>Send your MP our pre-written letter</h2>
<p>Unfortunately, your MP's email address is not listed in our database.</p>";
}
else {
echo "<h2>Send your MP our pre-written letter</h2>";
echo "<p>Please fill out the information required below:</p>";
**Insert Contact Form Here**
echo "<h2>Share this with your followers!</h2>";
echo 'Tweet ';
echo "<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>";
}
Thanks in advance for your help.
So, one option is to echo it out line by line, as your PHP code sample is doing, or in one big block. You can also combine PHP and HTML in the same file by starting and stopping the PHP interpreter. Here's an example of that:
if (empty($mpemail))
{
echo "<h2>Send your MP our pre-written letter</h2>
<p>Unfortunately, your MP's email address is not listed in our database.</p>";
}
else
{
?>
<h2>Send your MP our pre-written letter</h2>
<p>Please fill out the information required below:</p>
<form action="form.php" method="post" enctype="multipart/form-data">
<label></label>
<input name="name" required="required" placeholder="Your Name">
<label></label>
<input name="email" type="email" required="required" placeholder="Your Email">
<label></label>
<input name="address" type="name" required="required" placeholder="Your Address">
<label></label>
<textarea name="message" required > Dear Mr. X, Please support us... </textarea>
<input id="cancel" name="cancel" value="Cancel" />
<input id="submit" name="submit" type="submit" value="Submit">
</form>
<h2>Share this with your followers!</h2>
Tweet
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<?php
}
In a professional production application, the "right way" to do it is to separate your HTML from your PHP by putting the logic of your application in a separate PHP file, and then putting your HTML in a "template" which you load and display from PHP. Here is a bit of info about getting started with template engines and Model View Controller patterns in an application. But, for what you're trying to do, the example above of starting and stopping the interpreter should work for you.

Categories