So I'm a real noob to SQL. I only just managed to get my head around sending data from a html form to a table in a database.... Please bear with me.
I have a very large form on which I have broken down into 6 sections for each user to fill out. The project I am working on is a career progression based website. It has sections called "About Me", "Employment History", "Education" etc.
The about me page is the first form a use will complete and it will generate a new user account number in the database under the "About Me" table.
I want to take that number and use it on all other forms for the same applicant behind the scenes. How can I do that?
I hope that question made sense and thanks in advance for any help you can offer me.
What I have tried so far is this:
<?php
$ApplicantNo = SELECT ApplicantNo from aboutme WHERE EmailAddress='$EmailAddress' AND Firstname='$FirstName';
<input type="hidden" name='ApplicantNo' value='" . $ApplicantNo . "'>"; ?>
I would recommend adding this id to the session variable. Then it can be used on any page in the application.
It may be even better to have them create an account so the id can always be accessed even if session is ended. you would set the id in the session on each login
$_SESSION['uid']
Well to do that you have to use the SESSION variable..
First of all you have to execute the mysql query correctly
<?php
session_start();
$ApplicantNo = "SELECT ApplicantNo from aboutme WHERE EmailAddress='$EmailAddress' AND Firstname='$FirstName'";
$query=mysql_query($ApplicantNo);
$query_row=mysql_fetch_assoc($query);
$_SESSION['app_no']=$query_row['ApplicantNo'];
echo "<input type=\"hidden\" name='ApplicantNo' value='\" . $_SESSION['app_no'] . \"'>";
?>
So you can use it in this way in other pages and don't forget to write session_start() in begining of every page you use the SESSION variable.
Related
I have a drop down list on page one with select code:
print "Select week for season 1: <select name='Week_select'> <br>";
On page 2 I have
$varWeek=$_POST['Week_select'];
Then another drop down list:
print "Select a team that played season 1and week $varWeek: <select name='Team_select'><br>";
So far so good and many thanks to all who have gotten me this far.
Now when I go to page 3, I lose $varWeek
I see that I should either use a $_GET or pass it as hidden.
I tried $varWeek=$_GET['Week_select'];
but that didn't work.
I am unsure how to pass it hidden. Please help me understand a little more.
Many thanks in advance
A better approach would be to register those variables as session variables. This way they won't show up in the URL and you will be able to access them across several pages. Have a read here:
http://www.php.net/manual/en/intro.session.php
You can access/store a session variable like this:
$_SESSION['varname'] = 'value';
and on another page
var_dump($_SESSION['varname']);
Add the variable into the form, like you said, as a hidden field, like so:
print '<input type="hidden" name="Week_select" value="'. $_GET['Week_select'] .'" />';
Then on the page which handles the form, the variable will be available in $_POST['Week_select']
This might a little noobish but I don't know where to start with my problem. A friendly nudge in the right direction would be greatly appreciated.
I am trying to have a gallery of material, the images of the materials are stored in a database, now i want to make the users click on a specific image of the material that they like, this would lead them to next page that would calculate the price.
So i guess my question is, how do i carry this variable of their choice to the next page from the gallery?
Any help greatly appreciated
btw this is my code for getting the images displayed from the database:
<?php
mysql_connect("localhost","root","");
mysql_select_db ("imagez");
$res=mysql_query("select * from imgz");
while($row=mysql_fetch_array($res))
{
echo "<a href=\"page.php?id={$row['id']}\" />
<img src=\"{$row['img']}\> </a>";
}
?>
you can hand over values to another page by...
(1) QUERYSTRING (URL)
page.php?do=show&id=5
create those by pulling the unique id of a record from your db and create a link:
echo "<a href=\"page.php?id={$row['id']}\" />{$row['name']}</a>";
assuming that your table imgz has the fields id, name.
in page.php, you get those variables with
$_GET['do']; $_GET['id'];
pro: quick & easy
contra: can be easily manipulated by evil users - they change the querystring and get your script to do weird things.
(2) POST
create a <form method="post" action="page.php">with a hidden field named id containing the 'id' you want to pass. Add a <submit>-button to send the data to page.php.
in page.php, you get those values by:
$_POST['id'];
pro: safe, because evil user can't fiddle with POST
contra: overhead
(3) SESSION:
call session_start(); on top of every page, write your data into the session by...
$_SESSION['id'] = $id; $_SESSION['do'] = "show";
these variables will be kept between pages, in page.php you can simply use S_SESSION['id'] and have that value - if you called session_start().
pro: easy and safe, PHP will do the work for you (e.g. handling cookies etc.)
contra: will put load on your webserver if traffic goes up on your site.
You can set URL parameters, so when they click it, you would have a parameter like;
http://example.com/pricecheck.php?photoid=435
Then on the page you would do;
$photo = $_GET['photoid'];
Then you would have the ID of that photo on the page
Instead of passing the variable via URL, you can use cookie to store the variable and doing the calculation to check up.
<?php
$expire=time()+60*60*24*30;
setcookie("price", "$54", $expire);
echo $_COOKIE['price'];
?>
Now that variable $price would be available for each your webpages, and finally don't forget to clear the cookie `setcookie("hp","", $expire);
I have a form which sends the user to a different form incase they don't complete it to go back to the form and complete anything that was missed. Now unless the user presses the browser back button the page is loaded fresh which mean any data gets lost.
Pretty much when they come back to the previous page, anything they already filled in should stay intact.
I have the following two pages:
Page 1:
<?PHP
echo "<a href='page2.php'>NEXT</a>";
echo "<input type=text size=25 name=txt />";
?>
Page 2:
<?php
$refer = $_SERVER['HTTP_REFERER'];
$lastlink = "<a href='$refer'>BACK</a>";
echo $lastlink;
?>
On page two if i click BACK to come back to page 1, anything entered in page 1 will be lost which I do not want.
How do I work around it without using Javascript? with Javascript?
I know in javascript I can use
BACK
But is there another way?
Take a look at html5 history api. History api + ajax is great combination
You can also use a session to store $_POST on submit and check if it is set on page-load of the first form.
I am usually satisfied with:
history.back(-1);
Sending back to a previous page using Server-Side logic, is not a good approach to do, so I will disencourage it.
I am new to sessions, and think I get the basics of them, they seem to act like containers holding information which you can use on at a later stage and are linked to your UID.
I am wondering how I would save the ID of a page (example ID123) on the click of a button, and what exact code I'd have in the header.
Much appreciated in advance!
EDIT:
This is on wordpress, sorry to add this, each page has an 'event ID' I want this to be stored for use later (at a kind of checkout page) sorry for not adding this!
Saving page ID within $_SESSION
session_start();
$_SESSION['pageId'] = (int) $_GET['ID'];
Now you can use $_SESSION['pageId'] to get its value where you need.
Make sure to put session_start(); at top of your script where you use session-related functions or $_SESSION array.
To unset it when you don't need it anymore, you would do:
unset($_SESSION['pageId']);
session_destroy();
session_regenerate_id();
update
On button click you would do something like this:
var btn = document.getElementById('btnId');
btn.onClick = function() {
window.location = 'saveSession.php?id=xxx' // replace xxx with your id value
};
Now in saveSession.php you wll have to use code like shown above for storing it in session.
You can also use Ajax though.
In wordpress you cannot pass custom url parameters. So you cannot send something like
http://yourwordpresswebsite.com/?custom_param='value'
WOrdpress doesnt allow it. SO if all you want is a Post ID or a Page ID. It is easy to grab in wordpress. Do:
$val= $post->ID
Thats it and $val is set. The way I do if I have to send any custom parameters to a different file is by sending it as a form variable. There might be better ways but I am new to Wordpress and php too. So this is what I do:
<?php
echo "<form action='php_file.php' method='post' name='form_name'>
<input type='hidden' name='eventid' value='$event_id' />
<input type='submit' name='submit' value='submit'/>
</form>";
?>
If you want this to be your session info you can just add it in your php file where you are collecting the above fields.
session_start();
$_SESSION['pageId'] = (int) $_GET['eventid'];
/* Do not forget to check if the get variable is clean before you perform any operations, use mysql_real_escape_string(). It is a very important security measure. */
This might not be the best approach as I told you I am new to wordpress too.
I've been working on a way to build an archive for new threads. The over all goal was to make it so that if someone wanted to edit or delete a news thread they could, as well they could save a thread as a draft so that it ain't displayed to the public. I am using MySQL to store all the news threads, and I have it so that it prints out every news feed and the information for it. But when i click the edit button to edit that thread, it ALWAYS uses the id for the last MySQL entry called and NOT the ID I set it to use via a hidden form. Anyways here's the code and all parts to it. I'm so confused, and could really use some help. If you got questions just ask.
Main Script: http://pastebin.com/hn3cgVXu
Article_Post: http://pastebin.com/hhaLkuXe
Article_Archive: http://pastebin.com/X2fDg4dk
The original value for ID is called from the database, and set from article_archive
Display:
http://i25.photobucket.com/albums/c51/dog199200/Untitled-2.png
The Pencil is Edit, Trash Can is Delete. The image clearly shows that the loop is getting the ID, but that specific ID isn't being passed when the edit image is clicked.
In your Article_Archive when you loop through your database results you are naming your hidden input field the same thing for all the results.
<?php
while($row = mysql_fetch_array($news_list)) {
echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\" id=\"result_".$row['id']."\" name=\"result_".$row['id']."\">";
// ...
echo "... <input type=\"hidden\" name=\"id\" value=\"".$row['id']."\">";
// ...
echo "</form>";
} ?>
You're calling it id, so when you place multiple hidden input fields on the same form it will just grab the last one. Where is the javascript for when you click edit? You won't be able to do a standard form submit with that code since you're overwriting all the input fields with the same name attribute.