I am working on a simple quiz web app and i want to validate each and every question on submit button showing the correct and add marks for right questions.
I have created my first question page like this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Cric Quiz </title>
<link href="<?php echo base_url(); ?>assets/style.CSS" rel="stylesheet" type="text/css"/>
</head>
<body>
<div>
<h1>Cric Quiz</h1>
</div>
<div id='quesNo'>
<center>Question 01</center>
</div>
<div id='questions'>
<center>When did Australia win their first ever world cup?</center>
</div>
<div id='answers'>
<input type="radio" name="year" value="1992">1992<br>
<input type="radio" name="year" value="1996">1996<br>
<input type="radio" name="year" value="2000">2000<br>
<input type="radio" name="year" value="2012">2012
</div>
<center>Next</center>
</body>
</html>
My controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Questions extends CI_Controller {
public function question_1()
{
$this->load->view('quiz_one');
}
public function question_2()
{
$this->load->view('quiz_two');
}
}
How to validate my answers in my controller
PS: I'm using codeigniter
Your approach is wrong. Just have one function and pass the question id to that. If tomorrow, you have 1000 questions, will you create as many functions??
First store the correct answer in database.
Next your html should have the forms, submit the form with post data
Post the question id in form post
Add this code in controller,
// run query to fetch data from database based on the question in
if($this->input->post("year") == "value from database"){
} else {
}
Related
This question already has answers here:
php mysql storing line breaks in text area in database
(4 answers)
Closed 3 months ago.
i'm writing simple program that allow user to write their contents and post them
i'm using php ,html,css and mysql database
assume user has written a content in this format
i know codes written in php will allow user to post and store contents in database
i will use <?php echo $row['content']; ?> to display content posted by the user
but contents are not displayed in similary format as it was written by the use in paragraph formats(in format of more than one paragraph),insetad it display like this
Python datatypes.....To get Python handwritten notes, visit the link in the bio.....Visit our site for free project source codes-- copyassignment.com.....Turn on post notifications for more such posts like this.....Follow #python.hub for more content on computer science,
what's the good idea on this problem ? how can i solve it so that contents should be displayed as it is written by the user,,,
any help will be appreciated
<?php
require_once 'dbconfig.php';
if(isset($_POST['btnsave']))
{
$content = $_POST['content'];// user email
$stmt = $DB_con->prepare('INSERT INTO template(content) VALUES(:content)');
$stmt->bindParam(':content',$content);
$stmt->execute();
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-4">
<form method="post">
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">Description</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" name="content"></textarea>
</div>
<button type="submit" name="btnsave">ok</button>
</form>
<div>
<div>
<div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
</body>
</html>
<?php echo nl2br($row['content']); ?>
Use nl2br to convert Input/textarea linebreaks to real html linebreaks (br).
I'm trying to display set of values(hashtags) which I get from DB to View through controller.
But I'm stuck at here in this controller. Not sure what function to use, view or With after getting DB values. What will be best option to use? and how to use it in view
here is my sample code of Controller class
<?php
namespace App\Http\Controllers;
use App\Models\Hashtag;
use Illuminate\Http\Request;
use App\Http\Requests\SearchHashtagRequest;
class BannedHashController extends Controller
{
public function index(){
if(request()->query('query_hashtag')){
$hashtags = HashTag::where('hashtag','LIKE',"%{request()->query('query_hashtag')}%");
//Stuck in here
}else{
}
return view('bannedhashtags.index');
}
}
Index.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Instagram Hashtags</title>
</head>
<body>
<h1>Check your Instagram hashtags</h1>
<form method="get" type="get" action="{{url('bannedhashtags')}}">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for..." name="query_hashtag">
<span class="input-group-btn">
<button class="btn btn-secondary" type="submit">Go!</button>
</span>
</form>
</body>
</html>
you can do it like below:
public function index(){
if(request()->query('query_hashtag')){
$hashtags = HashTag::where('hashtag','LIKE',"%{request()->query('query_hashtag')}%");
return view('bannedhashtags.index',['hashtags'=>$hastags]);
}
return view('bannedhashtags.index');
}
and in this case you need to check for $hastags variable in your html like below
#if(isset($hashtags))
your html code here if the hashtags variable is set
#endif
if what you are looking for is to filter the hashtags list it will be better to show the hashtags list (maybe using pagination if the list is too big) and then filter it by using ajax request.
My goal from my code (using only HTML and PHP) is to provide a page for choosing the type of question (checkbox...) with the number of inputs and display the result in a different page by reading the 3 files that has been already saved in 3 main files (file for questions, file for number of inputs, file for options content), my idea is almost.
Here's my code from the displaying page :
<!doctype html>
<html>
<head>
<title>
form du reponse
</title>
</head>
<body>
<?php
//3 files
$k=fopen("choix multiples.txt","a+");//questions
$nn=fopen("nombre.txt","a+");//number of inputs
$no=fopen("les_options_multiples.txt","a+");//options content
//from here it only reads the first line of $nn and it keeps using it on the last loop
for($lol=fgets($nn);!feof($nn);$lol=fgets($nn))
{
for($questioncm=fgets($k);!feof($k);$questioncm=fgets($k))
{
echo "$questioncm :<br/>";
for($x=1;$x<=$lol;$x++)
{
echo fgets($no);
?>
<form action="" method="POST">
<div>
<input name="haha" type="radio" value="hahaha">
</div>
<?php
}
?>
</form>
<?php
}
}
</form>
</body>
</html>
I have created a simple register/login system and it (I think) works well so far.
Now I want to implement some of it's functions to my site.
controllers/main.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller
{
public function index()
{
$this->home();
}
public function home()
{
$this->load->view('home.php');
}
}
views/home.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Home</title>
</head>
<body>
<div id="container">
<?php if ($this->session->userdata('is_logged_in')): ?>
logged in as: <?php echo $this->session->userdata('username'); ?></br>
Logout
<?php else: ?>
not logged in</br></br>
<?php echo form_open('login'); ?>
<?php echo form_error('username'); ?>
<input type="text" name="username" placeholder="Login" />
</br>
<?php echo form_error('password'); ?>
<input type="password" name="password" placeholder="Password" />
</br></br>
<div><input type="submit" value="Sign in" /></div>
<?php echo form_error('login_validation'); ?>
</form>
<?php endif ?>
</div>
</body>
</html>
Is this the right way of doing things in CI?
For controller part instead of using the home(), you can directly load the view in index() because internally you are navigating from index to home() without any specific reason.
Also the loading view doesn't require ".php" extension in your file and you can do like this:
$this->load->view('home');
Where home.php is the file located inside application/views/
For showing the error messages, you can use another optimal way by defining the validation rules(in array fromat) inside config/form_validation.php and just require below code to show all messages instead one by one
<?php echo validation_errors()?>
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Could somebody please help me implement this HTML with my PHP login Page?
For some reason, the page is unresponsive.
Here is the HTML code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<link rel="stylesheet" href="css/screen.css" type="text/css" />
<!--[if ite - 8]><link rel="stylesheet" href="css/screen_ie.css" type="text/css" /><![endif]-->
<meta http-equiv="content-type" content="text/html; charset=windows-1251" />
<title>Index</title>
</head>
<body>
<div id="block">
<div class="logo"><img src="img/logo.png" alt="" /></div>
<div class="form">
<p class="login-box">Login box</p>
<div class="warning"><p>Wrong password, please try again.</p><p class="c"> </p></div>
<input type="text" value="Username"/>
<input type="text" value="*******"/>
<p class="btn">
<input type="button" value="Login"/>
</p>
<ul class="nav-path">
<li>Forgot your password?</li>
<li>Create new account</li>
</ul>
<i class="ctop"> </i>
<i class="cbottom"> </i>
</div>
</div>
</body>
</html>
And here is the current PHP login page with a temporary form:
<?php
include("db.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password sent from Form
$username=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
$password = sha1($password);
$sql="SELECT * FROM client_login WHERE Username='$username' and Password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
$row = mysql_fetch_array($result);
$client_ref = $row['Client_ref'];
$user_level = $row['user_level'];
// If result matched $username and $password, table row must be 1 row
if($count==1)
{
$_SESSION['user_level'] = $user_level;
$_SESSION['client_ref'] = $client_ref;
$_SESSION['user'] = $username;
if ($user_level == '1') {
header('Location: admin.php');
} else {
header('Location: myaccount.php');
}
}
else
{
echo "Error logging in!";
}
}
?>
<form action="login.php" method="post">
<label>UserName :</label>
<input type="text" name="username"/><br />
<label>Password :</label>
<input type="password" name="password"/><br/>
<input type="submit" value=" Login "/><br />
</form>
Any suggestions or ideas would be greatly appreciated.
There appears to be multiple problems with your code that you posted. For starters as I stated above in a comment, I don't recognize the conditional comment ite - 8. Make sure that comment is correct and use this site as a reference.
In addition, you are trying to send input with no form. A div with a class="form" is not the same as an actual form. Inside <div class="form"> add the following:
<form name="loginForm" id="loginForm" action="" method="post">
Fill in action with the page you are posting to.
After this line: <i class="cbottom"> </i> add a closing form tag like so:
</form>
Finally, change your button from type="button" to type="submit".
So to recap, check the conditional comment. This shouldn't stop your page from loading however. Your main problem was your lack of a form. When you try to submit inputs for posting you need to have a form. The form tells the page:
the method to use when submitting your request GET or POST
and the page to post to in the action="" tag
There are additional attributes that the form can provide but they aren't relevant to your login form.
Finally, in order for a form to be posted to the respective page, a submit action has to be fired. Buttons can have a type of button or submit. If you use type="button" it is up to you to wire up an event to trigger when the button is clicked in the onclick="" attribute.
For your purposes, you only need to submit your page and let the PHP do the rest. So for simplicity sake use type="submit" as it performs a submit of the page without you having to wire up an appropriate function.
Try these things first and let me know if you are still having issues. Happy Coding!
EDIT (ADDITIONAL ERRORS):
Another error I see is that your input fields do not have name attributes. When a form is posted the name attributes are sent through the respective stream (GET or POST). If no names are attached to the inputs, they will not be added to the stream.
It appears your PHP is expecting $_POST['username'] for the username input so add:
name="username"
to your username input field. It also appears that your PHP is expecting $_POST['password'] for the password field so add:
name="password"
to your password input. Also change the type on the password input from text to password. If you want the most basic of security and want anyone to join your site at least hide their password while they type it.
I recommend you start researching HTML and PHP by either purchasing a book on the topic(s) or by visiting a site such as W3Schools so you can learn and understand the basics of web design especially HTML as this is the base that you will be building on.
Use the form tag and put your php file in action or use javascript to submit your form.
<form action = "your_php_file.php">
//your sql queries
//don't forget to connect to database mysql_connect
</form>
P.S. I hope this might help you!