Jump to content

Chuks

Members
  • Posts

    7
  • Joined

  • Last visited

Chuks's Achievements

Newbie

Newbie (1/7)

0

Reputation

  1. Thanks, but I don't know how to provide it. I am only working with the w3schools.com tutorial on PHP - Ajax and PHP. If you. You would notice the file "gethint.php" is as in the tutorial.
  2. I was trying to use Ajax to to communicate with the server while a user types characters in the input field. After creating the HTML code, and the PHP file, on testing it I got the error message: "Warning: stristr(): empty needle in C:\wamp\www\php\revision\ajax\gethint.phponline 50". I don't understand or know how to get around the error. Please help ( ! ) Warning: stristr(): Empty needle in C:\wamp\www\php\revision\ajax\gethint.php on line 50 Call Stack # Time Memory Function Location 1 0.0002 385600 {main}( ) ...\gethint.php:0 2 0.0002 386544 stristr ( ) ...\gethint.php:50 ajax and php.php gethint.php
  3. Please, I am creating a user registration /login form to enable users to register and login. After registering with the username and password the user is redirected to the login form, but on logging in submitting the form it returns the login form with the password highlighted in red, although the password would be correct. How do I find out the problem with my code here: <?php // include config file require_once 'config.php'; // Define variables and initialize with empty values $username = $password = ""; $username_err = $password_err = ""; // Processing form data when form is submitted if($_SERVER["REQUEST_METHOD"] == ["POST"]) { // Check if username is empty if(empty(trim($_POST["username"]))) { $username_err = 'Please enter your username.'; } else { $username = trim($_POST["username"]); } // Check if password is empty if(empty(trim($_POST['password']))) { $password_err = 'Please enter your pasword'; } else { $password_err = trim($_POST['password']); } // Validate credentials if(empty($username_err) && empty($password_err)) { // Prepare a select statement $sql = "SELECT username, password FROM myguests WHERE username = ?"; if($stmt = mysqli_prepare($link, $sql)) { // Bind variables to the prepared statement as parameters mysqli_stmt_bind_param($stmt, "s", $param_username); // Set parameters $param_username = $username; // Attempt to execute the prepared statement if(mysqli_stmt_execute($stmt)) { // Store result mysqli_stmt_store_result($stmt); // Check if username exist, if yes then verify password if(mysqli_stmt_num_rows($stmt) == 1) { // Bind result variables mysqli_stmt_bind_result($stmt, $username, $hashed_password); if(mysqli_stmt_fetch($stmt)) { if(password_verify($password, $hashed_password)) { /* Password is correct, so start a new session and save username to the session */ session_start(); $_SESSION['username'] = $username; header("location: welcome.php"); } else { //Display error message if password is not valid $password_err = 'The password you entered is not invalid.'; } } } else { // Display error message if username doesn't exist $username_err = 'No acount found with that username.'; } } else { echo "Oops! Something went wrong. Please try again later."; } } // close statement mysqli_stmt_close(stmt); } // Close connection mysqli_close($link); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Login</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css"> <style type="text/css"> body {font: 14px sans-serif;} .wrapper {width:350px; pading: 20px;} </style> </head> <body> <div class="wrapper"> <h2>Login</h2> <p>Please fill in your credentials to login.</p> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <div class="form-group <?php echo (!empty($username_err)); ?>"> <label>Username:<sup>*</sup></label> <input type="text" name="username" class="form-control" value="<?php echo $username ?>"> <span class="help-block"><?php echo $username_err; ?></span> </div> <div class="form-group <?php echo (!empty($password_err)); ?>"> <label>Password:<sup>*</sup></label> <input type="password" name="password" class="form-control"> <span class="help-block"><?php echo $password_err; ?></span> </div> <div class="form-group"> <input type="submit" class="btn btn-primary" value="Submit"> <p>Don't have an account? <a href="register.php">Sign up now</a>.</p> </div> </form> </div> </body> </html> Here is the welcome page which I expect to be displayed: <?php // Initialize the session session_start(); // If session variable is not set it will redirect to login page if (!isset($_SESSION['username']) || empty($_SESSION['username'])) { header("location: login.php"); exit; } ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Welcome</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css"> <style type="text/css"> body {font: 14px sans-serif; text-align: center; } </style> </head> <body> <div class="page-header"> <h1>Hi, <b><?php echo $_SESSION['username']; ?></b>. Welcome to our site</h1> </div> <p><a href= "logout.php" class="btn btn-danger">Sign Out of Your Account</a></p> </body> </html> Please I am new in PHP and would appreciate your help. Thanks.
  4. Please, I desiged a contact form with validation, but on running the form the PHP validation codes occupy the input fields,and as well this Input:; echo $name; echo" "; echo $email; echo" "; echo $website; echo" "; echo $message; echo" "; echo $gender; ?> s appeared at the bottom . The following are the HTML and PHP codes: <!DOCTYPE html> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <?php // Define variables and set to empty values $nameErr = $emailErr = $genderErr = $websiteErr = ""; $name = $email = $website = $message = $gender = ""; if ($_SERVER("REQUEST_METHOD") == "POST") { if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name="test_input($_POST["name"]); } // Check if name only contains letters and white space if (!preg_match("/^[a-zA-Z]*$/" ,$name} { $nameErr ="Only letters and white space allowed"; } } if (empty($_POST["email"])) { $emailErr = "Email is required"; } else { $email = "test_input($_POST["email"]; } // Check whether email address is well formed if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr="Invalid email format"; } } if (empty($_POST["website"])) { $website == ""; } else { $website = test_input($_POST["website"]); } // Check if the URL address syntax is valid (this regular expression also allows dashes in the URL if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { $websiteErr = "Invalid URL"; } } if (empty($_POST["message"])) { $message = ""; } else { $messsage = test_input($_POST["message"]); if (empty($_POST["gender"])) { $genderErr = "Gender is required"; } else { $gender = tesr_input($_POST["gender"])); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>PHP Form Validation </h2> <p><span class="error">* required field.</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"> Name: <input type="text" name="name" value="<?php echo $name; ?>"> <span class="error">* <?php echo $nameErr;?></span><br><br> Email: <input type="text" name="email" value="<?php echo $email; ?>"> <span class="error">* <?php echo $emailErr;?></span><br><br> Website: <input type="text" name="website" value="<?php echo $website; ?>"> <span class="error"> <?php echo $websiteErr; ?></span><br><br> Comment: <textarea name="message" rows="4" cols="40"> <?php echo $message; ?>"></textarea><br><br> Gender: <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> Male<br><br> <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> Female<br><br> <span class="error">* <?php echo $genderErr;?></span><br><br> <input type="submit" name="submit" value="Submit"> </form> <?php echo <h2>Your Input:</h2>; echo $name; echo"<br>"; echo $email; echo"<br>"; echo $website; echo"<br>"; echo $message; echo"<br>"; echo $gender; ?> </body> </html> And here follows the output on running the above code: PHP Form Validation * required field. "> Name: * Email: * Website: Comment: <?php echo $message; ?>"> Gender: Male Female * Your Input:; echo $name; echo" "; echo $email; echo" "; echo $website; echo" "; echo $message; echo" "; echo $gender; ?> Please, I don"t know where my mistake is and need help to get the code work well.
  5. Please I am created a bootstrap contact form for my website, and now in the process of validating it before adding the PHP scripts,but don't figure out how to do it. Here is the form:<div class="container"> <div class="col-sm-12"> <div class="row"> <form id="contact-form" method="post" action="welcome2.php" role="form"> <div class="form-group has-error has-feedback"> <label for="name">Name</label> <input type="text" class="form-control" name="name" required="required" data-error="Name is required"> <div class="help-block with errors"></div> </div><br> <div class="form-group"> <label for="email">Email</label> <input type="email" class="form-control" id="email" placeholder="Enter your email*" required="required" data-error="Email is required"> <div class="help-block with errors"></div> </div><br> <div class="form-group"> <label for="comment">Message</label> <textarea class="form-control" rows="4" required="required" data-error="Please leave us a message"></textarea> </div> <button type="submit" class="btn btn-default">Submit</button> </form> </div> </div> </div> Already I have this php file: </head> <body> Welcome <?php echo $_POST["name"]; ?><br> Your email address is: <?php echo $_POST["email"]; ?><br> Your message: <?php echo $_POST["message"]; ?><br> </body> </html> Please my problem is how to display the entries in the input when the user clicks the submit button. On running the code it displays: Welcome Your email is: Your message:
  6. Here is the contact.php file: <?php $servername = "localhost"; $username = "root" $password = ""; // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if($conn ->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?> Thanks
  7. Please I have not been able to connect to MySQL. After opening a connect and checking the connection, I received the message: " 404 Not Found. The requested URL /contact.php was not found on this server. How do I go about this, I need help please. Thanks.
×
×
  • Create New...