Best way to capture page URL for quiz results page - php

Imagine taking a quiz at mysite/test/universe (URL = 'universe') or mysite/test/earth (URL = 'earth'). After you select the answers and click the Submit button, you're forwarded to a static page at mysite/test/results.php. The form automatically forwards some test data (e.g. $answer1 = $_POST['q1']).
Is there a way to modify the script so that, when someone clicks the Submit button, the page's URL is also captured and forwarded to the results page?
I don't want the URL itself to change. For example, if people are forwarded to mysite/test/results%universe, or something like that, then they could just visit that URL to see all the answers.
So I just want to forward the value of the URL, which I can then use to display and style correct and incorrect answers, along with feedback.
I should also explain that I'm working with PHP, jQuery and perhaps AJAX, and my ultimate goal is to modify my tests so that users can log in, and the test results will be published to a database. With that in mind, what's the best way to modify the HTML below so that the page URL is captured and forwarded to the results page?
<div id="quiz" rel="key" style="width: 500px; margin: 10px auto;">
<form action="grade.php" method="post" id="quiz">
<ol>
<li id="q1">
<div class="Question">No one knows if there’s just one universe or a series of universes, sometimes referred to as a:</div>
<div class="Answer">
<label for="q1-A"><div class="Radio"><input type="radio" name="q1" id="q1-A" value="A" style="display: none;"> A. multiverse</div></label></div>
<div class="Answer">
<label for="q1-B"><div class="Radio"><input type="radio" name="q1" id="q1-B" value="B" style="display: none;"> B. parallel universe</div></label></div>
</li>
</ol>
<input type="submit" value="Submit Quiz" />
</form>
</div>

Have a hidden field in your form
<input type="hidden" id="url" />
and set its value with this javascript
document.getElementById("url").value = document.URL;
or this jQuery
$(document).ready(function(){
$("input#url").attr("value", $(location).attr('href'));
});
then it will be submitted with the form.

you can use query string. if your first page url in http://something.html and second page url after form submit is another.html then
<form action="another.html?previous=<?php echo $_SERVER['REQUEST_URI']; ?>">
</form>
In another.html, you can get previous page url before submitting form by using GET
$_GET["previous"]

Related

How send slider values using $_POST

I have a simple form with two slider and two buttons created using links, because truthfully those links are images created with css.
So, I want to be able to send all slider values when you click the button and then retrieve those values in the second div to show it.
I want to do it using PHP, so I think the best way is to send over POST and then retrieve it using the same way.
</br></br></br>
<div class="main">
<form method="post" action="">
<input type="range" name="val1" id="val1" value="5" min="0" max="10" data-show-value="true"></br>
<input type="range" name="val2" id="val2" value="5" min="0" max="10" data-show-value="true">
</br>
</form>
</div>
<a class="btn1" href="#">Button 1 - View 1 (Sliders)</a></br>
<a class="btn2" href="#">Button 2 - View 2 (Results)</a>
<div class="second">
<p>--Second div---</p>
<p>--Value 1---</p>
<?php $_POST['val1'] ?>
<p>--Value 2---</p>
<?php $_POST['val2'] ?>
</div>
At first I want to know how to pass values between pages and then when you come back to the main view put the value selected before. I think this can be done using $_SESSION.
So, what do you suggest to get the values from the each slider and put it in the second div using PHP?
I want to develop a dynamic site when depends on the button that you click go to one page or another. I have nearly 20 possibilities depends on the button clicked, so I think that the best way is using divs and putting all the PHP and HTML code in a file and show or hide depends on the button that you click.
And I using bootstrap-slider library, but to do the example I used the jquery slider.
I've done a fiddle to see if someone is able to do it.
https://jsfiddle.net/dperezq/ph7mfu0v/

possible to GET parameters when you have POST the parameters?

i used a form to POST the parameters to categories.php
<form action="categories.php" method="POST">
<fieldset>
<legend>Filter Categories</legend>
<p>
<label><input type="checkbox" name="categories[]" value="music"/> Music</label>
<label><input type="checkbox" name="categories[]" value="technology"/> Technology</label>
<label><input type="checkbox" name="categories[]" value="film"/> Film</label>
</p>
</fieldset>
<button type="submit">Filter</button>
<button type="reset">Reset</button>
</form>
So if I would to filter Music and Film the URL would look like:
http://domain.com/categories.php?categories[]=music&categories[]=film
the parameters will be hidden since i used POST and thats how i want it i don't want to see the parameters BUT....i have some links in my site that link to single categories i.e.
Categories:
Music (links to http://domain.com/categories.php?categories[]=music)
Film (links to http://domain.com/categories.php?categories[]=film)
now when someone visits the categories.php page trough an URL like that it will give me errors because i USED POST and not GET now how will i be able to GET those parameters still?
Thanks
That is the problem when using POST to show the user some content.
It is impossible to bookmark or create a link so they can see it again without posting the same POST data every time.
Why is it that you don't want your users to be able to see the search terms on their address bar?
Since it's not a security problem I'm assuming it's because those links don't look good, in which case I'd point you to start using clean urls.

Two actions in a single form

I would like to say that I've already read all the similar questions, but did not find the answer I need.
So, I have the HTML form on the remote host that consists of username, password and "rememberMe" checkbox:
<form method="POST" action="http://1.2.3.4:5000/webman/login.cgi">
<p><input type="text" name="username" value="" placeholder="Username or Email"></p>
<p><input type="password" name="passwd" value="" placeholder="Password"></p>
<p class="remember_me">
<label>
<input type="checkbox" name="rememberme" id="remember_me">
Remember me on this computer
</label>
</p>
<p class="submit"><input type="submit" name="commit" value="Login"></p>
</form>
All I want to do is to submit data from one form to another one (another one - this is my Synology NAS Login form). But the problem is that if I write action="http://1.2.3.4:5000/webman/index.cgi", it does nothing (just sends me to the second login form).
But when I use action="http://1.2.3.4:5000/webman/login.cgi", it forwards me to the login.cgi page where only the following is displayed (with correct username & passwd)
{ "result" : "success", "success" : true }
BUT: if I change login.cgi to index.cgi in the browser, I go then to my desktop as I were logged in successfully via the default form.
So, on this basis, the question is:
How to send data to login.cgi, but redirect the user to .../index.cgi?
You need two forms. You can not have form in form. You need to copy data between forms with javascript. You can do that with this:
function copydata() {
document.form1.username.value = document.form2.username.value;
document.form1.passwd.value = document.form2.password.value;
return 1;
}
If I am getting this right you have login with two different actions. All you have to do is to apply this code once when secondary (submit for second form) submit button is clicked.
In the <form method="POST" action="http://1.2.3.4:5000/webman/login.cgi"> you need to add target="login_iframe" above to add the line <iframe id="login_iframe" name="login_iframe" width="0" height="0" frameborder="0" style="display: none;" ></iframe>.
Here is a script for redirecting the user after the verification. Notice that the script is only half working because there are no conditions "if the password is not correct then it should pop the inscription"
<script>
$(document).ready(function(){
$("#login_iframe").on("load", function(){
location.href = "https://the address of the page/";
});
})
</script>
You should change the redirection address.

how to focus on a particular section in another html or php page while clicking a button?

I want to use a search option to find the contents in another page.
My Html page:
<form action="search.php" method="post">
<input name="search" type="text"/>
<input type="image" src="images/search-btn.jpg" alt="search-btn" />
</form>
I have 5 topics in another page, say 'products.html'. Topic headings are Film, Music, etc.
So if a keyword, like the headings or some predefined keywords in each topic, is typed and search button is clicked I want to redirect to the products.php page.
The main thing is that i want to get the focus on that particular topic
Now I'm redirecting simply like this:
<?php
$val=$_POST['search'];
if($val=='Music'||$val=='singer')
{
header('Location:http://localhost/products.html');
}
?>
page1.html
<a name='music'></a><!-- This is anchor -->
<a href='#music'>Go to music on same page</a>
page2.html
<a href='page1.html#music'>Go to music on another page</a>
SERVER SIDE:
<?php
if($val=='Music'||$val=='singer'){
header("Location:http://localhost/products#$val.html");
}
?>
You can style your keywords anchors as you wish:
<a name='music'><b>music<b></a>
<span id='music'>music</span>
Within your form post to a page that can deal with the result, save it and then push the user on.
<form action="process.php" method="post" name="search">
This response page will redirect to something like
header("Location: /products.html#music");
Depending on how the form was completed.
Then on your products page add ids that tie this up, for example:
<h2 id="music">Music</h2>
The user will be redirected and the browser will jump to the corresponding anchor or id.
You can also try this technique by using a hidden field, but for the first page JavaScript will work for this
<input name="target" type="hidden" value=""/>
<input name="search" type="text" onkeyup="your-function-to-set-value-on-hidden-field"/>
Set the value if typed keyword is matched with your list and on the second page receive the hidden field value in PHP variable and do the stuff.

Avoiding duplicate code between php and javascript

I have a php page with a form on it for adding people to a small group.
For each person being added, there is a with multiple form elements, each named according to the person's number. For example:
<div class="user">
<input type="text" name="user1LastName" />
...
</div>
<div class="user">
<input type="text" name="user2LastName" />
...
</div>
For each person in the database, the php page populates a form sections.
To add additional people, the user can click on a "+" icon, at which time the page uses jQuery to dynamically populate a new . To do this I am simply appending the new div html to the existing form. This means that the javascript page contains all the same html markup (to be appended), as the php page.
This seems like an unnecessary duplication of code. Whenever I change something in the php page, I also have to change it in the javascript code.
Is there any general method for avoiding such code duplication? The only thing I can think of is to use jQuery to grab the html from an already existing div. But in this case, the values of the form fields for user n will appear in the new code for user n+1.
Thanks.
Capisci :)?
<div class="user" id="user_1">
<input type="hidden" name="uid[0]" value="1"/>
<input type="text" name="lastname[0]" value="user480029"/>
...
</div>
<div class="user" id="user_2">
<input type="hidden" name="uid[1]" value="2"/>
<input type="text" name="lastname[1]" value="arto"/>
...
</div>
Now when adding another field just...
<div class="user" id="user_3943094103945">
<input type="hidden" name="uid[]" value=""/>
<input type="text" name="lastname[]" value=""/>
...
</div>
Then you iterate trough $_POST[] a do what you want.
You have user ID on .user, so I you delete user you can remove that part of HTML (this is more for UX), more importantly, you don't have hundreds of variables but just a few array which you can iterate in one loop. Hope you get the point. Cheers.
The php code should give the javascript a "prototype", which could be modified using javascript. That way, even if there aren't any users, the javascript would still work. This example is obviously missing lots of code (like forms), but it should give you an idea. I haven't tested it because I assume you have to make lots of modification anyways.
<script type="application/x-javascript">
addEventListener("load",function(){
document.getElementById("add-user").addEventListener("click",function(){
var node=document.getElementById("prototype-container").getElementsByClassName("users")[0].cloneNode(true),n=document.getElementById("add-user").getElementsByClassName("users").length,list=node.getElementsByTagName("input");
document.getElementById("user-list").appendChild(node);
node.id="users_"+(n+1);
for(var i=0;i<list.length;++i)
list[i].name&&(list[i].name+="["+n+"]");
},false);
},false);
</script>
</head>
<body>
<div id="prototype-container">
<? /* print out a div without any information in it */ ?>
</div>
<div id="user-list">
<? /* print out divs with some infomation in them */ ?>
</div>
<button id="add-user">add a user</button>

Categories