Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is possible using JavaScript, but it's not recommended because some people turn JS off by default because of trojans and noisy behavior of some sites. It is considered polite to have your site working both with JS enabled and disabled.</p> <p>Actually you don't need many form actions because every operation can be done using branching in the single form handler script.<br> Here is the very simple CRUD application example, performing displaying, editing, adding - all in one body and utilizing templates: </p> <p>index.php </p> <pre class="lang-php prettyprint-override"><code>&lt;? mysql_connect(); mysql_select_db("new"); $table = "test"; if($_SERVER['REQUEST_METHOD']=='POST') { //form handler part: $name = mysql_real_escape_string($_POST['name']); if ($id = intval($_POST['id'])) { $query="UPDATE $table SET name='$name' WHERE id=$id"; } else { $query="INSERT INTO $table SET name='$name'"; } mysql_query($query) or trigger_error(mysql_error()." in ".$query); header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); exit; } if (!isset($_GET['id'])) { //listing part: $LIST=array(); $query="SELECT * FROM $table"; $res=mysql_query($query); while($row=mysql_fetch_assoc($res)) $LIST[]=$row; include 'list.php'; } else { // form displaying part: if ($id=intval($_GET['id'])) { $query="SELECT * FROM $table WHERE id=$id"; $res=mysql_query($query); $row=mysql_fetch_assoc($res); foreach ($row as $k =&gt; $v) $row[$k]=htmlspecialchars($v); } else { $row['name']=''; $row['id']=0; } include 'form.php'; } ?&gt; </code></pre> <p>form.php</p> <pre class="lang-php prettyprint-override"><code>&lt;form method="POST"&gt; &lt;input type="text" name="name" value="&lt;?=$row['name']?&gt;"&gt;&lt;br&gt; &lt;input type="hidden" name="id" value="&lt;?=$row['id']?&gt;"&gt; &lt;input type="submit"&gt;&lt;br&gt; &lt;a href="?"&gt;Return to the list&lt;/a&gt; &lt;/form&gt; </code></pre> <p>list.php</p> <pre class="lang-php prettyprint-override"><code>&lt;a href="?id=0"&gt;Add item&lt;/a&gt; &lt;? foreach ($LIST as $row): ?&gt; &lt;li&gt;&lt;a href="?id=&lt;?=$row['id']?&gt;"&gt;&lt;?=$row['name']?&gt;&lt;/a&gt; &lt;? endforeach ?&gt; </code></pre>
 

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