Multiple Submit Button in One Form

Ogos 8, 2006

Question: How do I put multiple submit button in one form, including some Javascript validation, in PHP or ASP?

Answer: Some of us find that we have to put more than one submit button in our HTML form. Let say, we have a product page, with the details in the input boxes that we could update them. In the same page, we also have a ‘DeleteThis Product’ button. And maybe, one more button, which will add some other things. This is how we do it:

First, we have to set the HTML form. Let’s make it like this. You can name it as index.php:

<HTML>
<head>
<title>Multiple Submit Button in One Form</title>
</head>
<body>
<form name=”myform” action=”index.php” method=”post”>
<input type=”hidden” name=”action” value=”">
<input type=”hidden” name=”product_id” value=”01″>
Product Name: <input type=”text” name=”product_name” value=”Product 1″><br />
Price: <input type=”text” name=”price” value=”USD1.00″><br />
Description: <textarea name=”desc” cols=”48″ rows=”8″>This is the description of the product</textarea>
Other Things: <input type=”text” name=”other” value=”This is the other things”>
<input type=”button” name=”submit_other” value=”Submit Other Things” onClick=”submitForm(document.myform, ‘other_things’);”><br /><br />
<input type=”button” name=”submit” value=”Save Changes” onClick=”submitForm(document.myform, ’save_changes’);”>
<input type=”button” name=”delete” value=”Delete Product” onClick=”submitForm(document.myform, ‘delete_product’);”>
/form>
</body>
</HTML>

Ok, now let’s find how to make sure each submit button will submit to its own action. This uses javascript, and should be placed in the <head> section.

<script>
function submitForm(form, action) {
form.action.value = action;
if (form.action.value == ’save_changes’) {
if (form.product_name.value == ”) alert(‘Please enter the Product Name!’);
else if (form.price.value == ”) alert(‘Please enter the Product Price!’);
else form.submit();
}
else if (form.action.value == ‘delete_product’) {
x = confirm (“Are you sure to remove this product?”);
if (x!=1)
return 0;
else {
form.submit();
}
}
else if (form.action.value == ‘other_things’) {
if (form.other_things.value == ”) alert(‘Please enter the other things!’);
else form.submit();
}
}
</script>

Remember, put it in the <head> section.

Next, the processing section. This example submits the form to its own page, called index.php. So we have to put the action code in this page, and should be placed on top.

For ASP users, see the example after this PHP code. Remember, change the index.php to index.asp.

<?
// assume that the database connection already set

switch ($_POST['action']){
case “save_changes”:
$q=”UPDATE product SET product_name=’$_POST['product_name']‘, price=’$_POST['price']‘, desc=’$_POST['desc']‘ WHERE id=$_POST['product_id']“;
$this->db->Execute($q);
break;

case “delete_product”:
$q=”DELETE FROM product WHERE id=$_POST['product_id']“;
$this->db->Execute($q);
break;

case “other_things”:
$q=”UPDATE product SET other_things=’$_POST['other']‘ WHERE id=$_POST['product_id']“;
$this->db->Execute($q);
break;
}
if ($_POST['action'] != ”) {
if ($this->db->Affected_Rows()) {
echo “<script>alert(\”Success.\”);</script>”;
} else {
echo “<script>alert(\”Failed.\”);</script>”;
}
}
?>

ASP users, copy this one:

<%
‘ you may use your own database connection
set conn=Server.CreateObject(“ADODB.Connection”)
conn.Provider=”Microsoft.Jet.OLEDB.4.0″
conn.Open “c:/webdata/database.mdb”

select case Request.Form(“action”){
case “save_changes”:
sql=”UPDATE product SET product_name=’”&Request.Form(“product_name”)&”‘, price=’”&Request.Fomr(“price”)&”‘, desc=’”&Request.Form(“desc”)&”‘ WHERE id=”&Request.Form(“product_id”)
conn.Execute sql
case “delete_product”:
sql=”DELETE FROM product WHERE id=”&Request.Form(“product_id”)
conn.Execute sql
case “other_things”:
sql=”UPDATE product SET other_things=’”&Request.Form(“other”)&”‘ WHERE id=”&Request.Form(“product_id”)
conn.Execute sql

end select
%>


PHP – Send Email with Attachments

Ogos 8, 2006

Question: How PHP send email with attachments?

Answer: You do not need to create or write a file for a text file, or html file, or php file, or even image file. All you need to do is think of a name for the file, make a link or button and point them to this script below:

<?php

$from_name = “My Name”;
$from_mail = “sender@email.com”;
$replyto = “sender@email.com”;
$subject = “This is the Subject Line”;
$message = “This is the message “;
$message .= “\r\n\r\nThis is the following message, in new line”;
$message .= “\r\n\tThis one is new line with tab”;
$mailto = “recipient@email.com;
$content = “the-content-to-be-attached–in-string”;
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = “From: “.$from_name.” <”.$from_mail.”>\r\n”;
$header .= “Reply-To: “.$replyto.”\r\n”;
$header .= “MIME-Version: 1.0\r\n”;
$header .= “Content-Type: multipart/mixed; boundary=\”".$uid.”\”\r\n\r\n”;
$header .= “This is a multi-part message in MIME format.\r\n”;
$header .= “–”.$uid.”\r\n”;
$header .= “Content-type:text/plain; charset=iso-8859-1\r\n”;
$header .= “Content-Transfer-Encoding: 7bit\r\n\r\n”;
$header .= $message.”\r\n\r\n”;
$header .= “–”.$uid.”\r\n”;
$header .= “Content-Type: application/octet-stream; name=\”the-file-name.txt\”\r\n”;
$header .= “Content-Transfer-Encoding: base64\r\n”;
$header .= “Content-Disposition: attachment; filename=\”the-file-name.txt\”\r\n\r\n”;
$header .= $content.”\r\n\r\n”;
$header .= “–”.$uid.”–”;
if (mail($mailto, $subject, “”, $header)) {
echo “<script>alert(\”Email has been sent.\”);</script>”;
} else {
echo “<script>alert(\”Email not sent.\”);</script>”;
}

?>

That’s all folks…


PHP – Force Download Dialog Box

Ogos 7, 2006

Question: How to force a download dialog box to appear when clicking a download link/button, instead of displaying it in the browser?

Answer: You do not need to create or write a file for a text file, or html file, or php file, or even image file. All you need to do is think of a name for the file, make a link or button and point them to this script below:

<?php

$filename = “filename.txt”;
$filecontent = “This is the text that will be written in the filename.txt. It could be anything in here, including the encoded characters of an image file.”;

header(“Content-disposition: attachment; filename=”.$filename);
header(“Content-type: text/TXT”);
header(“Pragma: no-cache”);
header(“Expires: 0″);
@set_time_limit(0);
echo $filecontent;
?>

That’s it. Easy, right? Good Luck!