Retrieve user ID displayed in the URL using PHP - php

I have a page where the admin can see all the members who are registered, that means their information are stored in the database. It is in tabular form with Update links displayed beside each name which redirects to my update.php page with forms. After I click the Update link, I am redirected to my update.php and the URL displays the user id which I need to update :clinic/update.php?res_id=3
Now, I have a form in my update.php and I need to get that res_id=3 displayed in the URL to also be included in my MySQL INSERT STATEMENT in my action script which is add_assessment.php. I have tried using $ID=$_GET['res_id'] and $ID=$_POST['res_id'] but none of which is working. Can anybody please tell me what line of code do I have to use? Thanks

Without having any of your code to go by, I can only suggest stepping through your workflow and using var_dump on both $_GET and $_POST and making sure that the res_id is being passed from page to page.

You are not passing the value to the form so the $_POST or $_GET values on update.php will not be available to add_assessment.php.
In clinic/update.php?res_id=3 output $_GET['res_id'] as the value in a hidden form field like so:
<input type='hidden' name='res_id' value='<?php echo htmlspecialshars($_GET['res_id']); ?>' />
Then access it in add_assessment.php with $_POST['res_id']
OR
Append it to the action='add_assessment.php' in the form like so:
<form action='add_assessment.php?res_id=<?php echo htmlspecialshars($_GET['res_id']); ?>' method='post'>
Then access it in add_assessment.php with $_GET['res_id']

Related

need to paas value from one page to another in php

am fresh to coding, so please advice if i had done any mistake.
I am having 2 pages, one am using to input the value and the other need to display the value.i had created the input page with one text box and submit button, once i click submit button the text box content should go to the next page and display the value.
this is the second page where values should come and display
enter image description here
This is the first page am entering values
enter image description here, in this i had add one submit button
Kindly help and advice
thanks in advance
you can use session in php to do it, in the first php file:
session_start();
$_SESSION['var']=yourValue;
now in the second php file:
session_start();
echo $_SESSION['var'];
First write your form with with get method like
<form method="get" action="second.php">
<input name="text_field_name1" value=""/>
<input name="text_field_name2" value=""/>
</form>
than you form value pass second page with url.
You can get value from second page by echo $_GET['text_field_name1']; and echo $_GET['text_field_name2'];

GET and POST is submitting on same time

I have html page and I have taken one form in it and other link outside the form .Form is Submitted by POST method,when I submitting form first time its ok and when I click link it pass data by GET method and when I again submit form then it send both GET and POST variable i.e form data and link data both.so what is the reason for that and how can I solve it.My html page is below
<html>
<body>
<form method='post'>
<input type=input name='name'/>
<input type=submit name='submit' value='submit'/>
</form>
<a href='check_global.php?page_number=6'>Page Number</a>
</body>
</html>
Because the form hasn't the action attribute, so it simply reload the page. When you submit it the first time it's all fine, but when you do it after clicking the link, the url is 'dirty' due to the data of the link, so you have both GET and POST values.
You can check wether the POST attribute is set ( if(isset($_POST['name'])) with php), in this case it has been submitted with the form
When you submit the form the second time you see the form parameters + the url parameter of the page (remember you clicked the link with the relative URL 'check_global.php?page_number=6').
To verify the above try this:
<?php
echo 'GET param ' . $_GET["page_number"];
echo 'POST param ' . $_POST["name"];
?>
As you can see you can access both types of parameters during a POST request.
Hope that helps.
Just to make the point, the OP did not indicate that the form was supposed to submit to anywhere but the current page. So just for funsies, here is the same basic idea, but with an action attribute value:
<form method="post" action="">
<input type="text" name="name"/>
<input type="submit" name="submit" value="submit"/>
</form>
Page Number
Notice that I've set it up so that, for whatever reason, the link points back to this same page and so does the form. The result:
First Load: form submit makes request with POST data to blah.php
Second Load: link follow makes request with GET (thanks to the query string) to blah.php?page_number=6
Third Load: form submit, using blank action to indicate that current page is where to post, makes request with POST form data to blah.php?page_number=6, thus having both POST form data and GET URL data.
So your options are to either set the action attribute value to blah.php so that it does not include the query string, or to accept that if you want to avoid the various ways of doing this in favor of having a more modular form (drop it in any page and you know it will post to that address), then to simply have the PHP backend check if $_POST['submit'] is set and if so, handle it like a form post and don't use any of the $_GET logic that might be screwing things up.
The link is never sending the form data as POST, and the POST data is not part of the GET array, so you know that when there is no POST, it's just get and if there is POST, it was a form submit, even if there is a GET array.
Or just use separate scripts so you don't get mixed up.

Cannot send data using form from index.php

I've run in to a small problem and I hope someone can help me out.
I made a form and it works fine:
http://www.volunteeringnews.com/formorg.php
If I hit send it returns a message saying User has been created.
So that works but if I go to http://www.volunteeringnews.com/ and under "Organisations" I click Submit it doens't work. And the Submit button is justa link to formorg.php.
I tried adding this to index.php but that was no success.
$action = isset($_POST['action']) ? $_POST['action'] : "";
//include database connection
include 'mysqli.php';
Can someone have a look?
Thanks!
The $_POST variable will be filled with the form data passed to it.
So on your form you'll have something like this:
<form action="http://www.volunteeringnews.com/formorg.php" method="post">
<input type='text' id='firstname' name='firstname'>
<input type='submit'>
Then when you submit that form, formorg.php will have the $_POST array populated with the names you gave the form fields. So, for example, to access whatever value the user filled out for the firstname field you'd access $_POST['firstname']
The first thing I can see is that the Action is not correct.
Normaly you should set the Action to the Script you were wanting the Post data to be:
<form action="http://www.volunteeringnews.com/formorg.php" method="post" border="0">
Otherwise you are going to direct the Post to the index.php were you are not getting the Request Values.

How to get url value in php?

I wanna make option edit product when user login.
Here is the link when user click edit on the web.
<a href="edit.php?id=<?php $id=$data['gameId'];echo $id;?>" ><input type="button" value="Edit"/></a>
Then in edit page, there's submit button, that I fill with some validation in doUpdate.php I tried to get id from URL in doUpdate.php to make an update in database (Id is the primary key, I use my sql as database) so I use this code
$id= $_GET['id'];
The problem is, why $id couldn't take the id value from the url? How should I fixed that? (It is not hosting yet, I use localhost cause I'm still learning)
If I'm understanding you correctly the problem isn't getting the id in edit.php, but getting the id in doUpdate.php. Presumably your edit page is POSTing date to the doUpdate page, in which case you need to do two things.
firstly inside your form on the edit page you'll need to add a hidden element inside you formcontaining the id you want to pass to doUpdate.php
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>"/>
and then in doUpdate.php fetch it from the $_POST global
$id = $_POST['id'];
You have to either edit the form action in your edit.php like <form name="edit" action="doUpdate.php?id=<?= $_GET['id'] ?> or you just create an input field, within the form, which has the value from your GET Parameter.
.
The code inside your <a> tag is incorrect. Also, you can't have a <button> inside an <a>. Try this:
Edit
It is possible for PHP to automatically set variables in this way using a feature called register_globals, but using this feature can open up security holes in your application which is why it is currently deprecated, and will eventually be removed from PHP.
The PHP manual has a detailed explanation of the feature and why it should not be used: http://php.net/manual/en/security.globals.php

HTML form insert in SQL DB and also pass posted vars to page

I have come across a situation I am not sure how to hndle. I am new to this, but I do understand the server side realm of php vs user side of the browser.. I just dont know how to accomplish what I want to do..
I have a form on a page where a user can enroll in a school course. They select the course type, location, date, and payment type.
On submit it goes to an outsourced shopping cart for payment, which uses PHP vars to populate the item description, price, ect.. along with our store id and other pertinent information.
I ALSO need to insert some of the PHP vars into the user database.
I tried having the form action go to another PHP page which process the DB entry then redirects to the cart, but when I get to the cart an error is generated saying the info was not submitted properly.. but the DB entry was successful.
I also tried using an include(dbentry.php) in the form action with the cart link.. this generates a server side error on loading the page.
At one point I successfully had it create a db entry (although it was blank) AND successfully redirect to the cart with all of the vars there, but a blank DB entry does me no good. I am assuming entry happened before the $POST vars were created... I also have changed so much I dont remember how I did it and cannot reproduce that..
My main question is:
How can I have a user fill out an HTML form, and when submitted perform the DB entry with the $POST vars while also directly passing the $POST vars to the cart page? Normally I would run the dbentry.php on the next page, but I have no access to scripting on the outsourced cart page...
You can try to use hidden textboxes to hold the values of the form! And this value can be accessed from different php pages
You will need to pass your variables from page1.php to page2.php to outsourced cart. I would do something like the following:
What the below code is doing:
Send original form data using POST to page2.php
Page2.php will then read the POST data (you can now do what you want with this data, whether it be store it into a database, etc.). A Javascript snippet will then submit the form to your checkout cart page (page3.php) with the necessary POST variables which are being stored as hidden fields within the form.
Page1.php
<form action="page2.php">
<input type="text" name="myfield1"/>
<input type="text" name="myfield2"/>
<input type="text" name="myfield3"/>
<input type="hidden" name="myfield4"/>
<input type="hidden" name="myfield5"/>
<input type="hidden" name="myfield6"/>
<input type="hidden" name="myfield7"/>
<input type="hidden" name="myfield8"/>
</form>
page2.php
<?php
if(isset($_POST['myfield1']))
{
$myfield1 = $_POST['myfield1'];
}
// do the above for all of the fields, use the data to query database with.
// Perform database operations here
?>
<form action='Page3.php' method='post' name='frm'>
<?php
foreach ($_POST as $a => $b) {
echo "<input type='hidden' name='".$a."' value='".$b."'>";
}
?>
</form>
<!-- Script to submit button -->
<script language="JavaScript">
document.frm.submit();
</script>

Categories