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

8
  • 2
    Please, stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO. 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 Commented Apr 16, 2015 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. Commented Apr 16, 2015 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. Commented Apr 16, 2015 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. Commented Apr 16, 2015 at 19:15
  • They are not my real login details ;) Commented Apr 17, 2015 at 6:14

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
you've got where id=$id2, which means you're probably fetching only a single record.
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
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.

Comments

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 !

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.