Dynamically appending data to the URL Query String while navigating between pages - php

This is just a project for my own learning purposes. For my project, users will be able to create polls and answer them. Each survey will have its own unique ID and can be accessed by its unique URL.
My problem is this. I know I can get around doing it this way, but this is a learning experience. When the user submits this form:
<form name="createPoll" action="polls.php" method="post">
Poll Title: <input name="pollTitle" type="text" /><br />
Option 1: <input name="pollOption" type="text" /><br />
Option 2: <input name="pollOption" type="text" /><br />
<input type="submit" value ="Submit Poll" /><br />
</form>
I want to run a script that will add this to my MySQL database and use the pollID that this script will generate for the URL like this:
http://www.mydomainname.com/polls.php?pollID=12345
where "12345" is the unique ID of the poll. My question is this:
What is the best way to bring the user to http://www.mydomainname.com/polls.php?pollID=12345 (assuming that the generated pollID will be '12345') on the submission of my form? Do I have to set the header in php in order to redirect them once the script has created the unique pollID?
Thanks

By the way: There is two inputs named "pollOption", be carefull :)
Your INSERT codes may be like that
$id=rand(100000,9999999999999999);
$checkid=mysql_query("SELECT ID FROM ID WHERE ID='$id'");
$checkidx=mysql_fetch_array($checkid);
if($_POST and $checkidx==null){
$title=$_POST['pollTitle'];
$opt1=$_POST['pollOption1'];
$opt2=$_POST['pollOption2'];
$insert=mysql_query("INSERT INTO ID,title,option1,option2 VALUES ('$id','$title','$opt1','$opt2')");
echo "Added! You re redirecting";
?>
<meta http-equiv="refresh" content="2;URL=http://www.mydomainname.com/polls.php?pollID=<?echo $id; ?>">
<?
}
And your polls.php may be like that;
$id=$_GET['id'];
if($id!=null){
$idget=mysql_query("SELECT * FROM blabla WHERE ID='$id'");
$idgetx=mysql_fetch_array($idget);
echo $idgetx[0] . $idgetx[1];
}

Generate the url, then use header() to redirect the page.
e.g.:
$id = 12345;
$url = 'http://www.mydomainname.com/polls.php?pollID=' . $id;
header('Location: ' . $url);

Related

How do i make form variable to display on all my site pages?

This is my form:
<form id="form1" name="form1" method="post" action="tracking.php">
<label>
<input type="text" name="trckno_trk" id="trckno_trk" />
</label>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</form>
When i submit my form, the submitted form variable displays well on the "tracking.php" page using <?php echo $_POST['trckno_trk']; ?>.
But when i click on other pages in the site, it doesn't seem to display. That mean that the form variable echo $_POST['trckno_trk'] displays only on one page but does not display on any other page.
Please, how can i get it to display on every other page on my site.
Try this
<?php
// Start the session
session_start();
?>
Then you html
<form id="form1" name="form1" method="post" action="tracking.php">
<label>
<input type="text" name="trckno_trk" id="trckno_trk" />
</label>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</form>
Then you should save the variable in your Session
<?php echo $_POST['trckno_trk'];
$_SESSION["trckno_trk"] = $_POST['trckno_trk'];
?>
Now you can use this Session to display on other pages.
Form submited data are available only for page you specified in action parameter. They are not stored in any way. You need to save them for example in database or SESSION variables and retrive them again when accessing other pages.
Form parameters are accesible only when you post them, to use them later you will need to save them somewhere.
If you'd like to save the value & display it everywhere for all clients, you will need to store it in a database (e.g. MySQL), fetch the value and print it
If you'd like it to be unique for each client, you could use cookies, for example:
setcookie('cookiename',
'the value',
time()+86400 /* seconds until it expires */,
'/' /* On what pages do you want to use it, '/' means all pages*/,
);
The code above saves the cookie, to print its value:
echo $_COOKIE['cookiename'];
Another option is to save it in the session, but it will expire after a short time
session_start(); // put this on the start of every page to start the session
$_SESSION['name'] = 'value'; // save a value in the session
echo $_SESSION['name']; // print it
I suggest you use $_SESSION[] to store your POSTED value, this way you will be able to access it on any other page.
So on your tracking.php you can have code like this
session_start();
$_SESSION['trckno_trk']=$_POST['trckno_trk'];
then on other pages you can access the variable using
session_start();
echo $_SESSION['trckno_trk'];

How to pass a GET Variable to a form

I have three pages. One of which there is a list of texts the user can select. Upon clicking on one of the texts they will be redirected to another page by using:
<a href='second.php?text=whatever>Whatever</a>
A page where they will input the username they wish to send those texts to - using forms. I wish to proceed to the third page with those two variable - texts and username. I only manage to proceed to third page with username only.
I am getting third.php?username=inputtedUsername.
I want to get third.php?username=inputtedUsername&&text=whatever.
I am aware that I can do by storing the text to a SESSION on page two and than transfer it over to third page.
I wish to know if there is another secure way to do this - maybe something needed to be changed in the form action=thirdpage.php? I dont know. Thank you. ö.ö.
Solved: After reading comments and answer, the thing I need was type=hidden. It is now working on my part. Thanks everyone for helping me. :).
'second.php?text=whatever'? You can't just put whatever to the text, you are doing it wrong. Try this.
firstpage.php
<?php
$whatever = 'Tom & Jerry, 1 + 2 = 3';
echo '' . $whatever . '';
?>
secondpage.php
<form action="thirdpage.php" method="post">
<input type="text" name="username" value="" />
<input type="hidden" name="text" value="<?php echo base64_decode($_GET['text']); ?>" />
<input type="submit" value="Submit" />
</form>
thirdpage.php
<?php
echo 'Username: ' . $_POST['username'];
echo '<br />';
echo 'Text: ' . $_POST['text'];
?>

Create a page with PHP and redirect to it

I have a form and the form method is set to post. It is connected with a second file which creates a session.
I would like to create a page with a unique url from that second file automatically with PHP and redirect to that page.
If possible insert some code to the created page.
This is the form code:
<form action="file_upload.php" method="post"
enctype="multipart/form-data">
Title: <input type="text" name="file_title" maxlength="55" required><br>
Description: <input type="text" name="file_description" maxlength="80" required><br>
<input type="submit" value="Title Magic">
</form>
This is the 2nd file code:
$title = substr($_POST['file_title'],0,55);
$description = substr($_POST['file_description'],0,80);
if(isset($_POST['file_title']))
$_SESSION['ses_file_title'] = $_POST['file_title'];
if(isset($_POST['file_description']))
$_SESSION['ses_file_description'] = $_POST['file_description'];
What I would like it to do is redirect to a page with a unique url and echo out these statements.
you have to get a value in hidden field after that forward it in $abc variable with if(isset()) condition,
after that for redirection use<script>window.location="filename.php?edt=<?php echo $abc ?>"</script> you'll get id with session on next page and use MySql to retrieve data for automatically created page... hope this will help
You can add yourcode in hidden field or use file_upload.php?code=yourcode and you can access it by request and use below php code for redirection.
header('Location: unique url ');

How to get url id from another page by form action page

i have a page where i am getting the ques id and insert it in the database for that i am doing
url: */faq/faq_question_sol.php?ques= 62*
this ( $selected_ques= ($_GET['ques']); ) is working properly in the *faq_question_sol.php* but the *answer_submit_process.php* does not recognize it
my form
<form id="post-form" class="post-form" method="POST" action="answer_submit_process.php">
<input id="submit-button" type="submit" tabindex="120" name="submitbutton" value="Post Your Answer" />
</form>
and the *answer_submit_process.php* is
if(isset($_POST['submitbutton'])){
$userid = $_SESSION['userid']; // i have already started the session
$selected_ques= ($_GET['ques']);
$content = $_POST["content"] ;
$query="INSERT INTO `formanswer`( `user_id`,`questionid`,`content` ) VALUES ('{$userid}','{$selected_ques}','{$my_html}' ) ";
$result=mysql_query($query);
}
Quickest solution would be saving the value of $_GET['ques'] on a hidden field of the form and thus make it accessible in answer_submit_process.php.
Something like this:
if (isset($_GET['ques'])){
echo '<input type="hidden" name="ques" value="'.$_GET['ques'].'">';
}
And in answer_submit_process page the value could easily accessed by $_POST['ques']..
If you are sending via a form POST, then the variable from which you can get data is $_POST instead of $_GET.
Anyway, i wasn´t able to find any field relating to the ques variable on your form, where are they?
Add <input type="hidden" name="ques" value="<?php echo $_GET['ques'] ?>"/> to your form to temporarily store the variable, and then use the variable $_POST['ques'] in place of $_GET['ques'] in the processing page.
Alternatively, you could change the form action to answer_submit_process.php?ques=<?php echo $_GET['ques']; ?>.

isset for link - PHP

I'm attempting to create a link for users to click that will remove them from a list. I'm trying to figure out how to do this without using a submit button and without using $_GET(if possible).
Anyway, I'm afraid to do it with $_GET (the way I have it now), because the user can type this in the URL (even though 99% wouldn't know how or think to do this) and they would be removed from the list.
How can I name the link so I can use $_POST?
$attendingUsers = mysql_query("Select acceptedInvites from events where eventID = ".mysql_real_escape_string($_GET['eventID'])." ");
$users= mysql_fetch_array($attendingUsers);
$user = $users['acceptedInvites'];
if(preg_match("/$userid/", $user)){
echo "You are attending this event</br>";
echo 'Click here to remove yourself from the list';
if($_GET['delete']=1){
$sql=...
}
}
Is it possible to do this without using $_GET? Thanks!
Never delete via a link. Read The Spider of Doom
Best way is to link to a "delete" page with an "are you sure" form. Submitting the form (via POST) performs the delete and redirects back to a suitable results page.
For example
Click here
to remove yourself from the list
Then, in remove.php
<?php
// get Event details via $_GET['eventID']
if (isset($_POST['confirm'])) {
// delete via SQL
// redirect
header('Location: http://example.com/events.php');
exit;
}
// display event details
?>
<form method="post" action="remove.php?eventID=<?php echo $eventId ?>">
<p>Are you sure?</p>
<input type="submit" name="confirm" value="Remove me from this event">
</form>
You should probably also look into CSRF protection but that's really outside the scope of this question.
Your are required to use either $_GET or $_POST
<form action="delete.php" method="post">
<input type="hidden" name="eventId" value="yourEventId" />
<a href="#" onclick="this.form.submit();" > Delete</a>
</form>
If I have my JavaScript right, this should do the trick:
Delete
<form id="delete" action="delete.php" method="post">
...
</form>
The link will then submit the form.
You could use some kind of encoding to make the get var unreadable, like an md5 or even an encrypted string.

Categories