Im doing an assignment and I got stuck.
I am preparing a project divided in 5 different .php files. The first page is an introduction page, where the user can type name and get started. 3 middle pages are for tests and quiz. The 5th and last page is a summary of the results of the previous tests. The second test is an <input type="checkbox" ...> and, according to the answer given, I want to print different results.
If the 2 answers are correct echo= "Correct"
If only 1 of the answers is correct echo= ""One of the two answers is wrong"
If only 1 box was checked echo= "Answer is incomplete"
No box was checked echo= "You did not select any option"
This is the HTML
<p>Through mixing which of the following primary colors can the secondary color in the figure be obtained?</p>
<form action="quiz_3.php" method="POST">
<label><input type="checkbox" name="result_quiz_2[]" value="yellow">Yellow</label>
<label><input type="checkbox" name="result_quiz_2[]" value="red">Red</label>
<label><input type="checkbox" name="result_quiz_2[]" value="blue">Blue</label>
<input type="submit" value="Confirm & Go to Quiz 3"><br>
With this code in the next page I store the answers in $_SESSION
<?php
session_start();
$q_2 = $_POST;
if (array_key_exists('result_quiz_2',$q_2))
{
$_SESSION['result_quiz_2'] = $_POST['result_quiz_2'];
}
else $_SESSION['result_quiz_2'] = 0;
?>
What should i write on the final file to print out the different results?
foreach ()
doesnt work if no boxes are checked and
isset()
will print "Correct" if just one answer of the two is correct.
really appreciate for whoever can help.
Related
I'm having a tough time figuring out how to do something which I think should be pretty basic but I just can't seem to find an answer that makes sense to my basic understanding of jquery.
Question 1:
I'm trying to generate a checkbox input that returns a value of 0 if left unchecked and returns a value of 1 if checked.
Here's how I figure I'd generate the checkbox:
<input type='checkbox' id='part_owner' name='owner/>
Here's how I figure I'd send the value:
item.find("input[name=owner]").attr("value",p.owner);
Question 2:
How would I retrieve the value, once saved to my database, and tell my checkbox to display accordingly?
Here's vaguely how I figure it would work:
owner: $("#dialog input[name=owner]").attr("value"),
Where owner is the field name being sent retrieved from the database via php.
Anyway, this question has been the bane of my existence for a duration that's slightly embarrassing. An explanation of this would be greatly appreciated, as I've apparently been having a hard time connecting the dots on this one.
Thanks in advance!
There are two options:
1. In HTML make one input:
<input type='checkbox' id='part_owner' name='owner' value="1" />
and in PHP make check:
if (!isset($_POST['owner'])) {
$_POST['owner'] = 0;
}
2. In HTML make just two inputs in exactly this order:
<input type='hidden' name='owner' value="0" />
<input type='checkbox' id='part_owner' name='owner' value="1" />
If checkbox will be checked, form will send value 1. Otherwise 0 will be sent.
When you use jQuery, you can detect if input is checked by:
if ($('#part_owner').is(':checked')) ...
Or check it with:
$('#part_owner').attr('checked', true);
We have a searchbox shown below where users can enter a value, say 1234 ABC street, click Search and the results get displayed.
<!--form1 -->
<div id="inputBG">
<form id="searchForm" class="search_field" method="get" action="">
<input name="searchBox" id="searchBox" value="3440 Mark Drive" />
<button type="button" onclick="searchMap()"><img src="Viewer/images/search.png" alt="Search" /></button>
</form>
</div>
This works great.
If, however, users wish to display more than one type of results, the user clicks on an icon called Advanced Search. When this happens, a form with only checkbox shown below opens up in a new window.
<!--Form2 --->
<div id="featuresDiv">
<form id="featuresForm">
<br/>
<input type="checkbox" name="srcoptions" onClick="checkAll(this.form,this)" checked>Check/Uncheck All<br>
<label><input type="checkbox" name="featType" checked value="Addresses">Addresses</label><br/>
<label><input type="checkbox" name="featType" checked value="Voter Precincts">Voter Precincts</label><br/>
<label><input type="checkbox" name="featType" checked value="Voter Precincts (Pending)">Voter Precincts (Pending)</label><br/>
<label><input type="checkbox" name="featType" checked value="Voting Polls">Voting Polls</label><br/>
<label><input type="checkbox" name="featType" checked value="Voting Polls (Pending)">Voting Polls (Pending)</label><br/>
<label><input type="checkbox" name="featType" checked value="Zip Codes">Zip Codes</label><br/>
<label><input type="checkbox" name="featType" checked value="Zoning Petitions">Zoning Petitions</label><br/>
<button type="button" onclick="searchMap()"><img src="MapViewer/images/magnifying_glass.png" alt="Search" /></button>
</form>
</div>
Users have the option of checking all boxes or just a couple of boxes.
When the user clicks on Search, the results of those checked boxes are supposed to be displayed.
In situations where checkbox option is used, and more than one boxes are checked, the searchbox becomes irrelevant in the search.
I have tried several things and nothing seems to work so far.
When I use the checkboxes option and check more than one options, I get custom message that "Your search returned no results"
Here is the Javascript we have attempted which is supposed to show when the search is from searchbox (form1) or checkbox (form2).
Then we have php that is supposed to grab all search values (searchbox or checkbox) in to comma-separated values.
This has been driving us nuts for over 3 days now.
It is noteworthy to point out that the searchbox has a default value of 3440 Mark Street.
I am really stumped.
Any help is greatly appreciated.
// Construct query
**$features = $_GET["featType"];**
$tsql = "SELECT * FROM globes AS TBL
WHERE KEY_TBL.RANK > 0
ORDER BY ListOrder, Name, TBL.RANK DESC";
Some more information about what happens in doGlobalSearch and how the data is processed in PHP would be helpful. Offhand I'd guess there's some discrepancy in how the data sent to PHP is formatted and how it is expected to be formatted. Taking a total stab in the dark, have you tried just doing featureList = features.join()? Are you sure the individual values need to be quoted?
Just a suggestion, but here's how I would do it:
Combine the two forms into one.
Default all checkboxes to unchecked (or disable them) while the advanced search div is hidden.
Check (or enable) them when the advanced search div is shown.
Add [] to the end of each checkbox name so that the (checked) values are passed to PHP as an array when the form is submitted.
This way PHP would directly receive the searchData and featTypes (if any) together and you wouldn't have to parse the featTypes back out from a string in PHP. Then all you would need to do with javascript is submit the form and process the response. Again, this is without knowing what's going on in the backend or what control you have over it.
I'm trying to make a section of my website to support gambling (with game money, not irl)
Goal: Create a simple Buy Ticket, Wait for tickets to be all gone, randomly select winner. So the end user can buy any amount of tickets left up to the total of 8 available tickets and then the script will choose a winner out of the number of applicants that bought a ticket.
Current Issue: My main issue is having problems with PHP storing and retrieving data.
<form action="games.php" method"post" name="add_tickets">
<ul>
<li>
<input type="radio" name="ticket" value="one"><br>
<input type="radio" name="ticket" value="two"><br>
<input type="radio" name="ticket" value="three"><br>
<input type="radio" name="ticket" value="four"><br>
<input type="submit" value="submit">
</li>
</ul>
</form>
So I'm using Radio buttons for this. When a button is selected and submitted, it will post the data over to games.php.
Then on games.php I'll have my script do its job and when its finished it will redirect the user back to index.php where the form was and have the user's name ($username) beside the radio button he selected.
Here is games.php code: NOTE: this isn't all the code... just a snippet on where my main issue is
if(isset($_POST['add_tickets'])){
$ticket = $_POST['one'];
$ticket = $_POST['two'];
$ticket = $_POST['three'];
$ticket = $_POST['four'];
}
$ticket_owners[] = "$ticket";
print_r($ticket_owners);
I'm trying to input which ticket they bought and their username into an array which then i'll run rand(array) to select a winner and return the answer.
Hmm, did I explain this okay? probably not... I wish I knew a little bit more about PHP... sigh
Can anyone shed some light on this... is this the wrong direction to take? Can it be done better with different methods?
Your code should have been
$ticket = $_POST['ticket']; // this will contain "one", "two", "three", or "four"
remove
if(isset($_POST['add_tickets'])){
}
and remove
$ticket_owners[] = "$ticket";
Now as to storing the username in a list that you will later use to select a random winner from... You will need something of a database or cache. As your array for example will cease to exist once the page is rendered and served to the user.
Since your inputs look like this <input type="radio" name="ticket" value="x">, you need to check $_POST['ticket'] and it will have on of the following values: none (variable won't exist) / one / two / three / four.
Where are you saving who bought what ticket? Where are you storing/receiving data, if you want help with that, please post code and elaborate in what you want to achieve and how you already tried
I want to have a multi-step form with HTML and PHP.
The first step of my form is an option like:
<input type="radio" name="service_type" value="plan1"> Plan 1<br />
<input type="radio" name="service_type" value="plan2"> Plan 2
Now, my question is: how can I know which option is selected so that I arrange the next step options for the user?
For example: If the user chooses option 1, next step would be: "You have chosen option 1, tell me who's your daddy". And if the user chooses option 2, next step says: "Welcome to option 2, tell me what you like", etc.
Now, I'm a totally beginner in PHP/HTML and know nothing about javascript. If you're answering this, I'd be so thankful, but please do it in an easy-to-understand sort of way.
I have already found this related to my case, but it is very hard to customize, and the validation process is of before CSS3.
[edit:]
Now I want to add a text-type input like this:
<input type="text" name="fname" value="firstname">
The guys told me to use $_POST['fname'] but for input texts, the 'value' property will show up inside the textbox like a default caption. I don't want this.
Now what do you suggest?
the the value from $_REQUEST:
$step = $_REQUEST['service_type']; // plan1 or plan2
In your PHP code, use the $_GET (or $_POST or `$_REQUEST - which gets either a GET or POST form) to return the value:
$serveiceType=$_REQUEST['service_type'];
As this is a radio button, only one value can be sent, and the sent value is easily accessible.
At first your input must be in a form tag. Now you can submit the form with an submit button(Input tag with type="submit").
In php you get the results with $_POST or $_GET.
<form method="POST">
<input type="radio" name="service_type" value="plan1"> Plan 1<br />
<input type="radio" name="service_type" value="plan2"> Plan 2
<input type="submit" />
</form>
<?php
$value = $_POST['service_type'];
echo $value;
?>
I need a little extra help to figure out an issue I'm having. You can see a great deal of detail about it here: Checking Correct Answer and Submitting Form, but here's the basics.
Project is a just-for-fun quiz composed of 10 question pages and 1 result page.
Submitting a Form composed of 3
Radio buttons, 1 is correct.
Security is not an issue.
Cannot use JavaScript, all PHP/HTML/CSS (mobile browsers and limitations etc.)
Form action takes the user to the next page.
How can I select the correct answer (can be hardcoded) and how can I save the number of right answers as a user continues through the pages?
<?php
/* Gets current correct answer Count */
$answer_count = $_GET["p"];
/* checks to see if the submitted answer is the same as the correct answer */
if ($_POST["submitted-answer"] == "correct-answer") {
$answer_count++;
}
?>
The form HTML:
<form name="quiz" action="" method="POST">
<label for="o1"><input type="radio" name="grp" id="o1" value="o1"> Label 1</label>
<label for="o2"><input type="radio" name="grp" id="o2" value="o2"> Label 2</label>
<label for="o3"><input type="radio" name="grp" id="o3" value="o3"> Label 3</label>
<input type="submit" value="Next Question" class="btn">
</form>
The values of my radio buttons are not necessary at the moment. Can I use something utilizing value="wrong" on the two wrong answers and value="right" on the correct one?
To track the number of correct answers you would probably want to use PHP sessions. Just add session_start(); at the beginning of the code, set some session variable to zero at the first question and destroy the session after the quiz is finished.
<?php
session_start();
if ([check if first question and unanswered]) {
$_SESSION['correct_answers'] = 0;
}
if ([check if question answered and correctly]) {
$_SESSION['correct_answers']++;
}
if ([check, if that was the final question]) {
[do some magic with the number of correct answers]
$_SESSION['correct_answers'] = 0; // no turning back now!
}
Something like that.