3

I have a form on one page linking to a PHP file (action), now the PHP result is being displayed in this PHP file/page. But I want the result to be displayed on the page with the form. I have searched thoroughly and couldn't find it anywhere. Perhaps any of you can help?

Code: /citizens.php (main page)

<form method="post" action="/infoct.php">
<input type="text" name="ID" placeholder="ID">
<input name="set" type="submit">
</form>

Code: /infoct.php

<!DOCTYPE html>
<html>
<head>
<!-- <meta http-equiv="refresh" content="0; url=/citizens.php" /> -->
</head>

<body>

<?php {
$ID2 = isset($_POST['ID']) ? $_POST['ID'] : false;
}

$connect = mysql_connect('localhost', 'root', 'passwd'); 
mysql_select_db ('inhabitants'); 
$sql = "SELECT `Name`, `Surname`, `DOB`, `RPS`, `Address` FROM `citizens` WHERE ID = $ID2";
$res = mysql_query($sql);
echo "<P1><b>Citizen Identification number is</b> $ID2 </p1>";
while($row = mysql_fetch_array($res))
{
    echo "<br><p1><b>First Name:  </b></b>", $row['Name'], "</p1>";
    echo "<br><p1><b>Surname:  </b></b></b>", $row['Surname'], "</p1>";
    echo "<br><p1><b>Date of birth:  </b></b></b></b>", $row['DOB'], "</p1>";
    echo "<br><p1><b>Address:  </b></b></b></b></b>", $row['Address'], "</p1>";
    echo "<br><p1><b>Background information:  </b><br>", $row['RPS'], "</p1>";
}
mysql_close ($connect);

?>
</body>
</html>

My fixed code thanks to Marc B

<form method="post">
<input type="text" name="ID" placeholder="ID">
<input name="set" type="submit">
</form>
<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$ID = isset($_POST['ID']) ? $_POST['ID'] : false;
    
$connect = mysql_connect('fdb13.biz.nf:3306', '1858208_inhabit', '12345demien12345'); 
mysql_select_db ('1858208_inhabit'); 
$sql = "SELECT `Name`, `Surname`, `DOB`, `RPS`, `Address` FROM `citizens` WHERE ID = $ID";
$res = mysql_query($sql);
if ($ID > 0) {
    echo "<p><b>Citizen Identification number is</b>  </p>";

    while($row = mysql_fetch_array($res))
    echo "<br><p><b>Surname:  </b></b></b>", $row['Surname'], "</p>";
    echo "<br><p><b>First Name:  </b></b>", $row['Name'], "</p>";
    echo "<br><p><b>Date of birth:  </b></b></b></b>", $row['DOB'], "</p>";
    echo "<br><p><b>Address:  </b></b></b></b></b>", $row['Address'], "</p>";
    echo "<br><p><b>Background information:  </b><br>", $row['RPS'], "</p>";

mysql_close ($connect);
}
    else {
      echo "<p>Enter a citizen ID above</p>";
    }
}
?>

Database snap DB Snap

Community
  • 1
  • 1
Demiën Drost
  • 174
  • 1
  • 3
  • 14
  • 2
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). If you want the data to "return" to the citizens page you might want to use AJAX. You're also in danger of [SQL Injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Apr 16 '15 at 15:17
  • Currently I am using these as these were thought to me by my teacher. I think I could change those... And thank you for suggesting. – Demiën Drost Apr 16 '15 at 15:19
  • 1
    Schools shouldn't be teaching "Old Skool" stuff when it comes to modern-day coding. Then they wonder how/why they get hacked. *Right Sam?* @JayBlanchard `mysql_`, old, and no security whatsoever. *"I think I could change those..."*, not "could", but **should/better**. Use prepared statements and read up on XSS injection. – Funk Forty Niner Apr 16 '15 at 15:23
  • As per your edit: I sure hope those aren't your actual login credentials. If they are, you better go and change your password, and FAST. – Funk Forty Niner Apr 16 '15 at 19:15
  • They are not my real login details ;) – Demiën Drost Apr 17 '15 at 06:14
  • @FunkFortyNiner would the real login credentials be exposed if this page were posted online? If someone viewed source, would the php appear as regular text? – DMop Jul 27 '18 at 03:08
  • @DMop no, as PHP functions Serverside they will not. After a request the server generates a HTML page which is sent to the client (browser), therefor not a single line of PHP code is transferred to the client. – Demiën Drost Jul 29 '18 at 20:53
  • @DemiënDrost that's great to know. Thanks a lot! – DMop Jul 30 '18 at 03:14

5 Answers5

7

A single-page form+submit handler is pretty basic:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
 ... form was submitted, process it ...
 ... display results ...
 ... whatever else ...
}
?>

<html>
<body>
<form method="post"> ... </form>
</body>
</html>

That's really all there is.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Thank you very much, but you made it work on my local server. Now I've uploaded it to the host, and it only shows the first array now (name). Do you happen to know a solution to this? – Demiën Drost Apr 16 '15 at 17:17
  • you've got `where id=$id2`, which means you're probably fetching only a single record. – Marc B Apr 16 '15 at 17:18
  • The ID is the ID of the "citizen" of which the information is being fetched. I changed the code and added it to the main post, also added a snap of the database. It did retrieve all the information when I used it on my localhost – Demiën Drost Apr 16 '15 at 17:25
2

Use code on the same page (citizens.php)

<?php

if (isset($_POST)) { 
Do manipulation
}
?>

Else use ajax and remove action method from form.

<form method="post" id="contactForm">
<input type="text" name="ID" placeholder="ID">
<input name="set" type="buttom" id="submitId">
</form>

<script>
$("#submitId").click(function(){
   var Serialized =  $("#contactForm").serialize();
    $.ajax({
       type: "POST",
        url: "infoct.php",
        data: Serialized,
        success: function(data) {
            //var obj = jQuery.parseJSON(data); if the dataType is not specified as json uncomment this
            // do what ever you want with the server response
        },
   error: function(){
        alert('error handing here');
      }
    });
});
</script>

And in your infact.php in the end Echo the data so that ajax will have the data in return.

Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
0

You could just put everything in infoct.php, like this:

<!DOCTYPE html>
<html>
<head>
<!-- <meta http-equiv="refresh" content="0; url=/infoct.php" /> -->
</head>

<body>
<form method="post" action="/infoct.php">
<input type="text" name="ID" placeholder="ID" value="<?php isset($_POST['ID']) ? $_POST['ID'] : '' ?>">
<input name="set" type="submit">
</form>
<?php 
    if (isset($_POST['ID'])) {
        $ID2 = $_POST['ID']; // DO NOT FORGET ABOUT STRING SANITIZATION
        $connect = mysql_connect('localhost', 'root', 'usbw'); 
        mysql_select_db ('inhabitants'); 
        $sql = "SELECT `Name`, `Surname`, `DOB`, `RPS`, `Address` FROM `citizens` WHERE ID = $ID2";
        $res = mysql_query($sql);
        echo "<P1><b>Citizen Identification number is</b> $ID2 </p1>";
        while($row = mysql_fetch_array($res))
        {
            echo "<br><p1><b>First Name:  </b></b>", $row['Name'], "</p1>";
            echo "<br><p1><b>Surname:  </b></b></b>", $row['Surname'], "</p1>";
            echo "<br><p1><b>Date of birth:  </b></b></b></b>", $row['DOB'], "</p1>";
            echo "<br><p1><b>Address:  </b></b></b></b></b>", $row['Address'], "</p1>";
            echo "<br><p1><b>Background information:  </b><br>", $row['RPS'], "</p1>";
        }
        mysql_close ($connect);
    }
?>
</body>
</html>

Do not forget about string sanitization !

0

I have found the solutions to the folowing problems:

Display results on same page

Thanks to Marc B

A single-page form+submit handler is pretty basic:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
 ... form was submitted, process it ...
 ... display results ...
 ... whatever else ...
}
?>

<html>
<body>
<form method="post"> ... </form>
</body>
</html>

That's really all there is.

Only first value is showing

I resolved this problem by adding this to my code:

while($row = mysql_fetch_array($res)) {
$surname=$row['Surname'];
$name=$row['Name'];
$dob=$row['DOB'];
$address=$row['Address'];
$RPS=$row['RPS'];

Now all the values are being displayed instead of only the first one.

Community
  • 1
  • 1
Demiën Drost
  • 174
  • 1
  • 3
  • 14
0

Display results on same page

Well I've stumbled upon this with the same problem and I found out you can simply require the other file. include_once("PATH_TO_FILE")'.

in /citizens.php

<?php include_once="infoct.php" ?>
<form> ... </form> 
<div>
 <?php $yourdata ?>
</div>

$yourdata should be html.

Do not forget about string sanitization !

Make sure to remove action from the form

Better than having all logic and Html in one file.