Trouble getting data from a URL in another PHP script
I have two PHP scripts. The first displays an HTML page based on a variable (noob.php?id=2). It has a form in it, and sends the data to the second script (not displayed to the user) which updates a database based on the given info. However, I need the "开发者_Python百科id" variable from the URL. I've tried using the $_GET['id']
function, but get an undefined index error.
In the form on the first page add:
<INPUT type="hidden" name="id" value="<?=$_GET['id']?>">
It seems that you have to scripts competing to read the same Query.String at the same time, and only one of the scripts is executed. I would suggest one of the following methods:
1- Direct the user to another page that has the other part of the script to handle the database
2- You can use the same page, but the first script can rename the variable so that the other script can read the value it is waiting for
3- You can use the same page, same variable name if you handle the Page Load event correctly, meaning that you need to decide which part of the script should run now, based on the user interaction with the form (If the user just visited the page, (No postback) then script1 should read the variable. Otherwise, script2 should get it.
Append a <input type="hidden" name="id" value="<?php $_GET['id'] ?>" />
into the form that you are creating. Then you can access it using $_POST['id']
on the script which receives the form data.
You mention that it sends it as a form. Perhaps you are sending it with the POST method?
To find out try this.
<?php
print_r($_GET); // All GET data. aka x.php?x=1
print_r($_POST); //Post data usually sent from form
print_r($_REQUEST); //everyhting
?>
Try below simple example which contains two php files 1> test.php 2> test2.php
1> test.php contains form which to post 2> test2.php echoing the id which passed to "test.php?id=1"
content of test.php
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form action="test2.php" method="POST">
<input type="hidden" value="<?php echo $_GET['id']; ?>" name="id" />
<table border="1">
<tr>
<td>Name : </td>
<td><input type="text" name="name" value=""></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
</body>
Content of test2.php
echo $_REQUEST['id'];
exit;
Try to call test.php like below
"Http://your.domian.com/test.php?id=1"
精彩评论