Demiën Drost
Demiën Drost

Reputation: 173

Form and PHP result display on same page

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

Upvotes: 3

Views: 37889

Answers (5)

Wajih Tagourty
Wajih Tagourty

Reputation: 1

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.

Upvotes: 0

Demi&#235;n Drost
Demi&#235;n Drost

Reputation: 173

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.

Upvotes: 0

Pratik Joshi
Pratik Joshi

Reputation: 11693

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.

Upvotes: 2

Razvan Stefanescu
Razvan Stefanescu

Reputation: 470

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 !

Upvotes: 0

Marc B
Marc B

Reputation: 360872

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.

Upvotes: 7

Related Questions