Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP query --> html form

Posted on 2003-10-22
8
Medium Priority
?
844 Views
Last Modified: 2013-12-16
Hi!

I do have a webpage with php+mysql.

So I do make a query from html form, but result of a query appears on a new page (page with all my content disapears and new content appears).

The question is: how to make it appear in the definite point (in the definit place of the page, on the right side of my form).


Thanks
0
Comment
Question by:Girigor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 17

Accepted Solution

by:
Suat Ozgur earned 172 total points
ID: 9597647
You can post the form itself and show retrieved data where you need on the HTML page.

You would have a submit button for this, and say its name is "frmsubmit".

In you PHP script you can check this value and retrieve data if it has been set.

Sample code:

if (isset($frmsubmit))
{
  //connect your database here and retrieve data, because if isset($frmsubmit) is true, it means user filled required information and clicked on the submit button (which is named as frmsubmit in this example)
}

Or as an alternative method, you can use a IFRAME in the page to show retrieved data, and when user clicks on the submit button you can reload the IFRAME by sending necessary data.

I hope it gives the idea.
Suat
0
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 9597656
BTW, I am not an exact PHP expert, just wanted to reply by considering "how I would do".
I hope easier or better methods come soon.

Suat
0
 
LVL 1

Assisted Solution

by:paStiSSet
paStiSSet earned 164 total points
ID: 9600086
you only need to use the include function:

<? include(phpfile.php); ?>

if you want to print results in somewhere of a page, you only need to put the include anywhere you want
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 2

Assisted Solution

by:errows
errows earned 164 total points
ID: 9631605
Use this sample code and modify it to fit your form fields:

<html>
    <head>
        <title>Form</title>
    </head>
<body>
<?php

//PHP_SELF is to post to the same page... don't have to bother with the filename.
echo "
<form name=\"test_form\" action=\"".$_SERVER["PHP_SELF"]."\" method=\"POST\" >
      <input type=\"text\" name=\"firstname\" />
      <input type=\"text\" name=\"lastname\" />
      <input type=\"submit\" name=\"submit\" value=\"Submit\" />
</form>\n";

//if the post var submit is set, it means the user clicked on Submit...
//do the echo wherever you want it to show on your page.
if (isset($_POST["submit"])){
    echo $_POST["firstname"]."<br />";
    echo $_POST["lastname"]."<br />";
}

?>
</body>
</html>
0
 

Expert Comment

by:nocell
ID: 9705037
can you give example with the HTML source codes of what you want? you don't need to paste the whole HTML files, only paste the bit where you want the result to appear.

Btw, how do you initiate the request to MySQL? I presume it is after you click a submit button right?

Example:
(Continue from what smozgur has said in the first reply. THis example submits a select query to mysql to retrieve a particular list of records. The example is extended from that given by errows)

At the start of your page you have:
<?php
if (isset($frmsubmit))
{
   call mysql_connect
   //Build the necessary query
   $query = "......."
   //Execute the query
   $result = mysql_query($query);
   if (mysql_num_rows($result) == 0)
      //Display message to indicate nothing was found. In this case, I have a page that contains HTML codes that
     //display a message for this kind of error. So, I just include that page. This is neat so that I just need to modify
     // the error page to get different error message or style. At the end of that error page, I have a single die("");
     // statement to terminate all other processing. By doing so, I ensure that only the error page will be printed out.
     // If you don't do that, all other codes after the include statement will be parsed and displayed as well.
     include('errorNoRecordFound.php');

   //Collect all records returned into array for easy access later
   $records = array();
   $num = 0;
   while ($row = mysql_fetch_array($result))
    {
        $records [$num] = $row;
        $num++;
    }
}

// If the parsing reaches this stage, that means record have been found. Then you can simply:
// I assume you will have field called firstname, lastname in your table where you get the data from.
// And this is the data you want to be displayed in the form. Also, I assume that the result will only
// contain one record. Such a case may be that you are retrieving details of user for them to view what is in your db.
// That is just my assumption.
<form name="test_form" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST" >
      <input type="text" name="firstname" value = "<?php echo $records[0]['firstname'] ?>>
      <input type="text" name="lastname" value = "<?php echo $records[0]['lastname'] ?>>
      //And a bunch of other details you want to display
      <input type="submit" name="frmsubmit" value="Submit">
</form>"

Now, hopefully you get the idea. You can select a bunch of records first and then echo-ing whatever you want in
the appropriate part of your form. You can use PHP to echo the whole HTML codes for each input element of a form
but I found that is a bit too much work because you have to backslash ". Besides, having the HTML codes with PHP
embedded allow you to modify the HTML using HTML editor such as Dreamweaver easier.

If you want an update page, you can add an update query before the select query inside the if statement above.
Then you have an update page which update the content stored in database and displays the updated data.

Regards,
Nocell
0
 

Expert Comment

by:nocell
ID: 9705039
can you give example with the HTML source codes of what you want? you don't need to paste the whole HTML files, only paste the bit where you want the result to appear.

Btw, how do you initiate the request to MySQL? I presume it is after you click a submit button right?

Example:
(Continue from what smozgur has said in the first reply. THis example submits a select query to mysql to retrieve a particular list of records. The example is extended from that given by errows)

At the start of your page you have:
<?php
if (isset($frmsubmit))
{
   call mysql_connect
   //Build the necessary query
   $query = "......."
   //Execute the query
   $result = mysql_query($query);
   if (mysql_num_rows($result) == 0)
      //Display message to indicate nothing was found. In this case, I have a page that contains HTML codes that
     //display a message for this kind of error. So, I just include that page. This is neat so that I just need to modify
     // the error page to get different error message or style. At the end of that error page, I have a single die("");
     // statement to terminate all other processing. By doing so, I ensure that only the error page will be printed out.
     // If you don't do that, all other codes after the include statement will be parsed and displayed as well.
     include('errorNoRecordFound.php');

   //Collect all records returned into array for easy access later
   $records = array();
   $num = 0;
   while ($row = mysql_fetch_array($result))
    {
        $records [$num] = $row;
        $num++;
    }
}

// If the parsing reaches this stage, that means record have been found. Then you can simply:
// I assume you will have field called firstname, lastname in your table where you get the data from.
// And this is the data you want to be displayed in the form. Also, I assume that the result will only
// contain one record. Such a case may be that you are retrieving details of user for them to view what is in your db.
// That is just my assumption.
<form name="test_form" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST" >
      <input type="text" name="firstname" value = "<?php echo $records[0]['firstname'] ?>>
      <input type="text" name="lastname" value = "<?php echo $records[0]['lastname'] ?>>
      //And a bunch of other details you want to display
      <input type="submit" name="frmsubmit" value="Submit">
</form>"

Now, hopefully you get the idea. You can select a bunch of records first and then echo-ing whatever you want in
the appropriate part of your form. You can use PHP to echo the whole HTML codes for each input element of a form
but I found that is a bit too much work because you have to backslash ". Besides, having the HTML codes with PHP
embedded allow you to modify the HTML using HTML editor such as Dreamweaver easier.

If you want an update page, you can add an update query before the select query inside the if statement above.
Then you have an update page which update the content stored in database and displays the updated data.

Regards,
Nocell
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question