I have php page with two forms. One for searching and the other one for updating the searched data. When I updated data it wont show the search form. How can I show the search form after I summited the data. Please help me. My script is below

<html>
<head>
    <meta name="description" content="Php Code Search & Display Record" />
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
    <?php
    // Find out which form/action we need to load. I have created some
    // hidden input fields to help identifying the forms.
   // echo '<p>The following data has been submitted:</p><pre>'; print_r($_POST); echo '</pre>';

    if(!$_POST['form_name'])
    //if(!$_POST['search'])
    {
        //* No search query has been submitted yet. Display the search form.
        ?>
        <form name="search" method="post" action="search.php">
            <input type="hidden" name="form_name" value="search">
            <table style=" border:1px solid silver" cellpadding="5px" cellspacing="0px" align="center" border="0">
                <tr>
                    <td colspan="3" style="background:#0066FF; color:#FFFFFF; fontsize: 20px">
                        Search
                    </td>
                </tr>
                <tr>
                    <td width="140">Enter Search Keyword</td>
                    <td width="240"><input type="text" name="search" size="40" /></td>
                    <td width="665"><input type="submit" value="Search" /></td>
                </tr>
                <tr bgcolor="#666666" style="color:#FFFFFF">
                    <td>Record ID</td>
                    <td>Description</td>
                    <td> </td>
                </tr>
            </table>
        </form>
        <?php
    }
    elseif($_POST['form_name'] == 'search' && $_POST['search'])
    {
        //* The search form has been submitted. Display the update form(s).
        $search = $_POST["search"];
        $result = mysql_query("SELECT * FROM saudi_journal WHERE SO like '%$search%'")or die(mysql_error());
        while($row = mysql_fetch_array($result))
        {
            ?>
            <form  action="search.php" method="post">
                <input type="hidden" name="form_name" value="update">
                <input type="text" name="RID" value="<?php echo $row['RID'];?>" size=6>
                Author
                <input type="text" name="AU" value="<?php echo $row['AU'];?>" size=15>
                Title
                <input type=text name="TI" value="<?php echo $row['TI']; ?>" size=30>
                Source
                <input type=text name="SO" value="<?php echo $row['SO']; ?>" size=40>
                Fulltext Link
                <input type=text name="FULTEXT" value="<?php echo $row['FULTEXT']; ?>" size=40> 
                <input type=submit name="submit" value="submit"></p><br>
            </form>
            <?php
        }
    }
    elseif($_POST['form_name'] == 'update')
{
    //* The update form has been submitted. Update the database.
    // Let's see if we actually arrive at this part:
    $RID = $_POST['RID'];
    $AU = $_POST['AU'];
    $TI = $_POST['TI'];
    $SO = $_POST['SO'];
    $FULTEXT = $_POST['FULTEXT'];
// In your query, $FULTEXT is probably null, or does it get defined somewhere else?
$query = "UPDATE saudi_journal SET AU ='$AU' , SO='$SO', FULTEXT='$FULTEXT' WHERE RID = '$RID'";


//echo '<p>The query that was executed, is as follows:<br>' . $query . '</p>';
mysql_query($query);
$error = mysql_error();
if($error)
    echo $error;
else

    echo "<center>Successfully Updated in DATABASE</center>";


    **
// I need the search form back after this messege
}



    ?>


</body> 
</html> 

Recommended Answers

All 3 Replies

The search form should use the GET method, because it's a reading instance, the update form a POST method because it writes to the database. So you can do:

# read
if($_SERVER['REQUEST_METHOD'] == 'GET')
{
    # execute search
    if(isset($_GET['search']))
    {
        # code and populated form
    }

    else
    {
        # blank form
    }
}

# write
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if(isset($_POST['update']))
    {
        # execute update query & redirect to referer page
    }
}

The redirect after a POST request is important, otherwise, by refreshing the page it will be submitted another time. Check header():

Thank you Sir. I will try and see it. can you please edit in my script and repost it. I am not an expert to do this.

I put my form after the php query. then it work. Any way thanks for replying

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.