Post and get at the same time in php
Do you have any suggestions with my problem. I need to use get and post at the same time. Get because I need to output what the user has typed. And post because I need to access the mysql database in relation to that input. It looks something like this:
<form name="x" method="get" action="x.php">
<input name="year" type="text">
<select name="general" id="general">
<font size="3">
<option value="YEAR">Year</option>
</form>
This will output the contents of mysql depending on what the user will check:
<form name="y" method="post" action"y.php">
<input name="fname" type="checkbox">
</form>
And the form action of those two combined will look something like this:
<?php
if($_POST['general'] == 'YEAR'){
?>
<?php echo $_GET["year"]; ?>
<?php
$result2 = mysql_query("SELECT * FROM student
WHERE student.YEAR='$syear'");
?>
<table border='1'>
<tr>
<?php if ( $ShowLastName ) { ?><th>LASTNAME</th><?php } ?>
<?php if ( $ShowFirstName ) { ?><th>FIRSTNAME</th><?php } ?>
</tr>
<?php while ( $row = mysql_fetch_array($result2) ) {
if (!$result2) {
}
?>
<tr>
<td><?php echo $row['IDNO']?> </td>
<td><?php echo $row['YEAR'] ?> </td>
<?php if ( $ShowLastName ) { echo('<td>'.$row['LASTNAME'].'</td>'); } ?></td>
<?php if ( $ShowFirstName ) { echo('<td>'.$row['FIRSTNAME'].'</td>'); } ?>
I really get lots of undefined errors when I do this. What can you recommend that I should do in order to get the value inputted by the user together with the mysql data.
You can only have one verb (POST, GET, PUT, ...) when doing an HTTP Request. However, you can do
<form name="y" method="post" action="y.php?foo=bar">
and then PHP will populate $_GET['foo']
as well, although the sent Request was POST'ed.
However, your problem seems to be much more that you are trying to send two forms at once, directed at two different scripts. That is impossible within one Request.
You cannot do a GET and POST at the same time.
Combine the two forms into one.
For example combine the forms to one 'post' form. In your code extract whatever you need from $_POST.
And 'YEAR' does not equal 'Year', your sample code also needs work.
As saig by the other answers, you can't do a get and a post request at the same time. But if you want to unify your PHP code in order to read a variable received through a get or post request, maybe you could use $_REQUEST