Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate table with values from a form on the same page
    primarykey
    data
    text
    <p>This is the code of my homepage:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;PHP test&lt;/title&gt; &lt;link href="style.css" rel="stylesheet" type="text/css"&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="formDiv"&gt; &lt;form action="testsql.php" method="post"&gt; Email: &lt;input type="text" name="email"&gt;&lt;br&gt; &lt;textarea name="comment" rows="10" cols="40"&gt;Your comment here.&lt;/textarea&gt; &lt;input type="submit" value="submit"&gt; &lt;/form&gt; &lt;div id="show"&gt; &lt;?php $con=mysqli_connect("","root","","my_db"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM Comments"); echo "&lt;table border='1'&gt; &lt;tr&gt; &lt;th&gt;Email&lt;/th&gt; &lt;th&gt;Comment&lt;/th&gt; &lt;/tr&gt;"; while($row = mysqli_fetch_array($result)) { echo "&lt;tr&gt;"; echo "&lt;td&gt;" . $row['Email'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['comment'] . "&lt;/td&gt;"; echo "&lt;/tr&gt;"; } echo "&lt;/table&gt;"; mysqli_close($con); ?&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>and here is the code of the testsql.php file:</p> <pre><code>&lt;?php $con=mysqli_connect("","root","","my_db"); if($_SERVER["REQUEST_METHOD"] == "POST") { header("Location: testphp.php"); if(empty($_POST["comment"])) {echo "You have to write something. &lt;br&gt;";} else { if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } if(empty($_POST["email"])) { $sql="INSERT INTO Comments (Email, comment) VALUES ('$_POST[email]', '$_POST[comment]')"; echo "1 record added."; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } } else{ if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$_POST["email"])) { echo "Invalid email format &lt;br&gt;"; } else{ echo "Your email is: {$_POST["email"]} &lt;br&gt;"; $sql="INSERT INTO Comments (Email, comment) VALUES ('$_POST[email]', '$_POST[comment]')"; echo "1 record added."; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } } } } } ?&gt; </code></pre> <p>What I want to do is to take the values from the form and put them in the table. The table should be updated after I click the submit button. It works when I have the <code>header(Location: "testphp.php");</code> but then it won't show the error messages if the user writes his mail the wrong way or doesn't write a comment. </p> <p>When I don't include the header the code works right but I get redirected to <em>testsql.php</em> and I have to go back to <em>testphp.php</em> to see the updated table. I know that I can use javascript but our professor told us that the <strong>site should be fully usable with javascript turned off</strong>. Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload