allow users to make changes to database tables in laravel - php

I am a beginner in programming and this is what i am trying to make: I am making a web application with questions and answers of certain problems. I have a login screen where you have to put in a specific code to access the website. If code is not entered, it wont let you access. There is also another code you can put in. That code will let you access another page where the user can edit the questions and answers. My Question is: Is there any way can you allow users to edit your database tables via a web application?
The only thing i have in that page are 2 buttons which say: (change table) or (add/delete table)
<div id="container">
<div class="button1">
<form action="" method="get">
<input type="button" name="submit" value="Add/Delete table"/>
</form>
</div>
<div class="button2">
<form action="" method="get">
<input type="button" name="submit" value="Change table"/>
</form>
</div>
</div>

of course you can, for each table you want to edit (questions and answers here I guess) you will need :
A controller
A model
A request
Authorizations
This is the minimum, you can add any customization then.
Reading the doc is a good starting point, it's very well written https://laravel.com/docs/9.x

Related

Sliding questions using PHP and JQuery

I have a simple website, I want it to ask 10 questions and save the answers to a database.
I would like it to be a little interactive and only display one question at a time. then when the user answers that question, the div slides to the left and displays the next question until all questions have been asked and the page then submits the answers to a database. The answers would be saved under the users IP address.
So this is how I envision it working:
Q1: What is your name?
User enters details (eg Tony) and then presses next, the database populates as follows:
UserID: 192.168.0.1 QuestionID: 1 Answer: Tony
Then the user is shown question 2.
What is your favourite colour?
User answers the question and selects next. Database populates:
UserID: 192.168.0.1 QuestionID: 2 Answer: Blue
Eventually getting to the final question and clicking submit.
I can do the submit etc, but I am struggling to find a way to implement a multiple div layout that looks smooth and elegant.
Any ideas?
<div><h1>Question 1: What is your name?</h1></div>
<div><input type="text" name="yourName" class="inputBox" value="" required /></div>
A method that uses jquery and display none class:
JS
function next(this_q, next_q) {
// SAVE DATA TO DB VIA AJAX ETC...
$("#q"+this_q).toggleClass("d-none");
$("#q"+next_q).toggleClass("d-none");
}
HTML
<div id="q1">
This is question 1?
<input ... >
<button onclick="next(1,2);" ... >
</div>
<div id="q2" class="d-none">
This is question 2?
<input ... >
<button onclick="next(2,3);" ... >
</div>
<div id="q3" class="d-none">
This is question 3?
<input ... >
<button onclick="next(3,4);" ... >
</div>
more divs ...
Obviously style etc is missing but I'd do it kinda like this.
When the next button is clicked:
saves the answer -> hides the answer -> shows the next one

Submitting multiple textareas using jquery

I am building a page where a user (teacher) can assing students homework tasks. Depending on how many tasks are assigned, the number of comment boxes will change.
I have a page in which multiple forms are created. The number of textareas vary as described above. All student answers are shown on the same page. A comment box is shown for each individual task, in order to enable the teacher to respond to every type of mistake (if any) the student make.
Each comment field is related to a specific ID (the student answer for that specific task), but how do I most economically go about creating an array and submitting the specific textarea 'on change'. I.e. I want the database comment field to be dynamically updated when a teacher is done commenting on a specific student answer.
I am not a complete novice, but far from being proficient in java (jquery and ajax, which I use to handle most of my site). I use php and MySQL for serverside and jquery for client side data handling.
I have not really begone coding yet as I am looking for a good and viable approach. I have between 30-40 tasks assigned on avarage.
<div id="defaultPortlet<?php echo $portL; ?>" class="panel-collapse collapse">
<div class="portlet-body clearfix">
<form>
<textarea class="col-lg-12" rows="3">
<?php echo $row_dbResponce['ten_notes']; ?>
</textarea>
</form>
</div>
</div>
I would like to update the database on change (i.e. usig ajax handle data and calling a php database update page).
Any help on how to approach this problem would be usefull.
change
<textarea class="col-lg-12" rows="3">
to
<textarea class="col-lg-12" rows="3" id="descriptor<?php echo $portL; ?>">
Then in your jquery...
$("textarea[descriptor^='news']").each(function( index ) {
$(this).change();
})

add / remove field dynamically in php mysql without javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am working on student database. Each Applicant can apply for upto three programs. I want to include add / remove program option on my php form without using javascript. Is it possible to do it wihout javascript?
please help!!!
You can do simple form to add info to Database with Submit Button that's for adding . For Deleting you can make Delete page that GET user id that you want to delete .
PHP only executes at runtime, ie when the page first loads. To do this, you'd need to reload the page every time you wanted a change to the page. So what I see is you have the first page they land on offer a form for the user to choose which of your three programs they're interested in, when the form submits, the page reloads and the server - queuing off the user's selection - loads and displays the appropriate program form.
If you want to do it without reloading the page, it is not possible without JavaScript as PHP cannot execute without talking to the server.
Here's an example of a form that at first displays a question to the user, posts to itself, then conditionally displays a field based on the user's input:
<?php if (!isset($_POST['program'])) : ?>
<form method="POST" action="">
Should we show the program field?<br>
<input type="radio" value="y" name="program" id="program_y"><label for="program_y">Yes</label><br>
<input type="radio" value="n" name="program" id="program_n"><label for="program_n">No</label><br>
<input type="submit">
</form>
<?php else : ?>
<form method="POST" action="form_submit_url_here">
<label for="name">Name:</label>
<input name="name" type="text" id="name"><br>
<?php if ($_POST['program'] === 'y') : ?>
<label for="program_name">Name of your program:</label>
<input type="text" name="program_name" id="program_name"><br>
<?php endif; ?>
<input type="submit">
</form>
<?php endif; ?>
Short answer: you can't.
PHP requires information to be processed either by that page or by another, and therefore data needs to be sent. Javascript (using AJAX) can be used to send that data from the page without a page refresh or relocation.

posting twice to the same page

I have a form to submit details to database, after processing the POST on the action page i have another form to upload a photo very closely related to the info provided on the previous form, in fact the image path is stored on the same record in the database, instead of having two pages / two steps process, is it possible to have them both on the same form?
i know that nesting forms is not possible, at the same time uploading the file requires a form.
Using anchors and GET method is not acceptable in my application for the info is too sensitive to be revealed in URL
is there a way to workaround this?
thanks in advance
You could use
either session variables (to temporarily store the first step of the form)
or javascript to cycle through steps without refreshing the page
How about using 2 fieldsets?
<form action="?">
<fieldset>
//input fields
<input type="button" value="Next" id="btnNext">
</fieldset>
<fieldset>
//foto input field
<input type="button" value="Submit">
</fieldset>
</form>
Then in JS (with jQuery):
$("fieldset").eq(1).hide();
$("#btnNext").click(function(e){
e.preventDefault();
$("fieldset").eq(0).hide();
$("fieldset").eq(1).show();
});

Using PHP IF ELSE for online treasure hunt

I'm trying to create an online treasure hunt game where users have to go to specific sites to get the clues and enter it on a website. There are 7 questions whereby upon getting a correct answer, the user will see the next question and clue.
So far this is my PHP code.
<?php
//task 1
if(strtolower($_POST["task1"]) == "hello") {
?>
Here's your next clue!
<p><form action="quiz2.php" method="post">
<input type="text" name="task2" />
<input type="submit" />
</form>
<?php
} else {
?>
Wrong!
<p><form action="quiz.php" method="post">
<input type="text" name="task1" />
<input type="submit" />
</form>
<?php
}
?>
I know how to do it in multiple pages but I would like it if I can do it in one PHP page itself. The reason being, users can just view source the page and just skip to the next question/clue without answering.
How do I achieve this in one PHP page?
There are a lot of ways to handle this. My preference?
Allow users to define a unique id (fun name or something) to track progress.
Have a small db store ids and progress.
Use AJAX to post and retrieve data.
This approach would require one HTML page with AJAX methods, one PHP controller that accepts the unique id and and answer. If the answer is correct, the PHP controller records the status to the DB allowing the controller to return the next clue. If a user logs off the page and comes back, they just have to put in their ID and pick up where they left off. This is a fine solution if security isn't an issue, and it doesn't sound like it is.
Happy coding!

Categories