Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post and get at the same time in php

Tags:

php

mysql

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.

like image 244
user225269 Avatar asked May 01 '10 09:05

user225269


People also ask

Can I use GET and POST at the same time in PHP?

Now, there can be occasions where, you need to have HTTP POST forms to get data from users. The question is, can you use GET and POST method together to create different dynamic pages from a single script? The answer is yes!

Can I use GET and POST at the same time?

With Ajax you can send data to a PHP script via GET and POST in the same request. The process is similar to sending data through the POST, it's actually the same, being only applied a "trick".

How can use both GET and POST method in HTML?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.

When we use GET and POST method in PHP?

There are 2 HTTP request methods: GET: Requests data from a specified resource. POST: Submits data to be processed to a specified resource.


2 Answers

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.

like image 62
zaf Avatar answered Sep 20 '22 00:09

zaf


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.

like image 26
Gordon Avatar answered Sep 19 '22 00:09

Gordon