PDA

View Full Version : how can i convert this php script to javascript?



alapimba
09-30-2008, 05:26 PM
Hello

i did a php script to validate a form but the person that i need to give this don't want to jump to other page to validate the form, so i guess the way to go it's javascript but i'm completly lost.

Anyone can tell me how to convert this to a javascript that don't jump to another page, if it could hightligh the fields that need to be filled then it would be perfect.

Here is my code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Edi 2008</title>
</head>

<body>
<form action="inscrever.php" method="post" name="form1" id="form1" style="margin-bottom: 0">
<table width="760" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="logo.jpg" width="254" height="88" hspace="5" /></td>
<td align="right" valign="bottom"><span class="style1">Ficha de inscri&ccedil;&atilde;o</span></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="100%" border="0" cellpadding="5" cellspacing="1">
<tr>
<td bgcolor="#047EC7"><div align="center" class="style6">Dados da Entidade</div></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="100%" border="0" cellpadding="0" cellspacing="5" class="style2">
<tr>
<td width="12%"><label>Empresa*</label></td>
<td colspan="3"><input name="empresa" type="text" class="style5" id="empresa" /></td>
</tr>
<tr>
<td><label>Morada*</label></td>
<td colspan="3"><input name="morada" type="text" class="style5" id="morada" /></td>
</tr>
<tr>
<td><label>Código Postal*</label></td>
<td width="34%"><input name="codigo" type="text" class="style5" id="codigo" /></td>
<td width="9%"><label>Localidade*</label></td>
<td width="45%"><input name="localidade" type="text" class="style5" id="localidade" /></td>
</tr>
<tr>
<td><label>Telefone*</label></td>
<td><input name="telefone" type="text" class="style5" id="telefone" size="30" /></td>
<td><label>Fax*</label></td>
<td><input name="fax" type="text" class="style5" id="fax" /></td>
</tr>
<tr>
<td><label>Contribuinte*</label></td>
<td colspan="3"><input name="cont" type="text" class="style5" id="cont" /></td>
</tr>
<tr>
<td><label>Contacto*</label></td>
<td><input name="contacto" type="text" class="style5" id="contacto" /></td>
<td><label>E-mail*</label></td>
<td><input name="email" type="text" class="style5" id="email" /></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="100%" border="0" cellpadding="5" cellspacing="1">
<tr>
<td bgcolor="#047EC7"><div align="center" class="style6">Dados da(s) pessoa(s) a inscrever</div></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="100%" border="0" cellpadding="0" cellspacing="5" class="style2">
<tr>
<td width="180">Nome*</td>
<td width="180">Cargo*</td>
<td width="180">E-mail*</td>
<td width="8%">Valor</td>
<td colspan="2" align="center"><div align="center">
<p>Participa<br>
Jantar/Espect&aacute;culo</p>
</div></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td align="center">sim</td>
<td align="center">n&atilde;o</td>
</tr>
<tr>
<td><label>
<input name="nome1" type="text" class="style5" id="nome1" />
</label></td>
<td><label>
<input name="cargo1" type="text" class="style5" id="cargo1" />
</label></td>
<td><label>
<input name="email1" type="text" class="style5" id="email1" />
</label></td>
<td>Gratuito</td>
<td align="center"><label>
<input name="jantar1" type="radio" value="sim">
</label></td>
<td align="center"><label>
<input name="jantar1" type="radio" value="nao">
</label></td>
</tr>
<tr>
<td><label>
<input name="nome2" type="text" class="style5" id="nome2" />
</label></td>
<td><label>
<input name="cargo2" type="text" class="style5" id="cargo2" />
</label></td>
<td><label>
<input name="email2" type="text" class="style5" id="email2" />
</label></td>
<td>Gratuito</td>
<td align="center"><label>
<input name="jantar2" type="radio" value="sim">
</label></td>
<td align="center"><label>
<input name="jantar2" type="radio" value="nao">
</label></td>
</tr>
<tr>
<td>Mais participantes</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td><label>
<input name="nome3" type="text" class="style5" id="nome3" />
</label></td>
<td><label>
<input name="cargo3" type="text" class="style5" id="cargo3" />
</label></td>
<td><label>
<input name="email3" type="text" class="style5" id="email3" />
</label></td>
<td>120,00&euro;</td>
<td align="center"><label>
<input name="jantar3" type="radio" value="sim">
</label></td>
<td align="center"><label>
<input name="jantar3" type="radio" value="nao">
</label></td>
</tr>
<tr>
<td><label>
<input name="nome4" type="text" class="style5" id="nome4" />
</label></td>
<td><label>
<input name="cargo4" type="text" class="style5" id="cargo4" />
</label></td>
<td><label>
<input name="email4" type="text" class="style5" id="email4" />
</label></td>
<td>120,00&euro;</td>
<td align="center"><label>
<input name="jantar4" type="radio" value="sim">
</label></td>
<td align="center"><label>
<input name="jantar4" type="radio" value="nao">
</label></td>
</tr>
<tr>
<td colspan="6">As inscri&ccedil;&otilde;es no evento j&aacute; incluem IVA &agrave; taxa de 20%.</td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="100%" border="0" cellpadding="5" cellspacing="1">
<tr>
<td bgcolor="#047EC7"><div align="center" class="style6">Dados para facturac&atilde;o</div></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="100%" border="0" cellpadding="0" cellspacing="5" class="style2">
<tr>
<td><label>Entidade</label></td>
<td colspan="3"><input name="entidade1" type="text" class="style5" id="entidade1" /></td>
</tr>
<tr>
<td><label>Morada</label></td>
<td colspan="3"><input name="morada1" type="text" class="style5" id="morada1" /></td>
</tr>
<tr>
<td width="12%"><label>Código Postal</label></td>
<td width="21%"><input name="codigo1" type="text" class="style5" id="codigo1" /></td>
<td width="10%"><label>Localidade</label></td>
<td width="57%"><input name="localidade1" type="text" class="style5" id="localidade1" /></td>
</tr>
<tr>
<td><label>Contribuinte</label></td>
<td colspan="3"><input name="contribuinte1" type="text" class="style5" id="contribuinte1" /></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="100%" border="0" cellpadding="5" cellspacing="1">
<tr>
<td bgcolor="#047EC7"><div align="center" class="style6">Forma de pagamento</div></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="100%" border="0" cellpadding="0" cellspacing="5" class="style2">
<tr>
<td valign="top"><label></label>
<label>
<input name="radiobutton" type="radio" value="cheque">
</label></td>
<td>Por cheque:<br />
Endere&ccedil;ado &agrave; DIFERENTES MEIOS, Lda.<br />
Pra&ccedil;a Nuno Rodrigues dos Santos, 7<br />
1600-171 Lisboa</td>
</tr>
<tr>
<td valign="top"><label></label>
<label>
<input name="radiobutton" type="radio" value="Transferencia ....

alapimba
09-30-2008, 05:28 PM
the php

<?php
$data = date("d/m/Y");

$sendTo = "r.machado@bla.com";
$subject = "edi2008";

$empresa = $_POST['empresa'];
$morada = $_POST['morada'];
$codigo = $_POST['codigo'];
$localidade = $_POST['localidade'];
$telefone = $_POST['telefone'];
$fax = $_POST['fax'];
$email = $_POST['email'];
$cont = $_POST['cont'];
$contacto = $_POST['contacto'];

$nome1 = $_POST['nome1'];
$cargo1 = $_POST['cargo1'];
$email1 = $_POST['email1'];
$jantar1 = $_POST['jantar1'];
$espectaculo1 = $_POST['espectaculo1'];
$nome2 = $_POST['nome2'];
$cargo2 = $_POST['cargo2'];
$email2 = $_POST['email2'];
$jantar2 = $_POST['jantar2'];
$espectaculo2 = $_POST['espectaculo2'];
$nome3 = $_POST['nome3'];
$cargo3 = $_POST['cargo3'];
$email3 = $_POST['email3'];
$jantar3 = $_POST['jantar3'];
$espectaculo3 = $_POST['espectaculo3'];
$nome4 = $_POST['nome4'];
$cargo4 = $_POST['cargo4'];
$email4 = $_POST['email4'];
$jantar4 = $_POST['jantar4'];
$espectaculo4 = $_POST['espectaculo4'];

$entidade1 = $_POST['entidade1'];
$morada1 = $_POST['morada1'];
$codigo1 = $_POST['codigo1'];
$localidade1 = $_POST['localidade1'];
$contribuinte1 = $_POST['contribuinte1'];

$jantar1 = $_POST['jantar1'];
$jantar2 = $_POST['jantar2'];
$jantar3 = $_POST['jantar3'];
$jantar4 = $_POST['jantar4'];

$pagamento = $_POST['radiobutton'];

function verificaCampos($campo1,$campo2,$campo3){
if($_POST[$campo1] != "" || $_POST[$campo2] != "" || $_POST[$campo3] != ""){
if($_POST[$campo1] != "" && $_POST[$campo2] != "" && $_POST[$campo3] != ""){
return 1;
}
else{
return 0;
}
}
else{
return 1;
}
}

$t2 = verificaCampos("nome2","cargo2","email2");
$t3 = verificaCampos("nome3","cargo3","email3");
$t4 = verificaCampos("nome4","cargo4","email4");

/*function verificaCampos2($campo1,$campo2,$campo3,$campo4,$campo5){
if($_POST[nome3] != "" || $_POST[nome4] != ""){
if($_POST[$campo1] != "" && $_POST[$campo2] != "" && $_POST[$campo3] != "" && $_POST[$campo4] != "" && $_POST[$campo5] != ""){
return 1;
}
else{
return 0;
}
}
else{
return 1;
}
}

$t22 = verificaCampos2("entidade1", "morada1", "codigo1", "localidade1", "contribuinte1");*/

if( !eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@([a-z0-9]+([\.-][a-z0-9]+))*$",$email)){
echo "Por favor, preencha um e-mail válido<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($_POST["empresa"] == "") {
echo "Por favor preencha os dados em branco sobre a entidade<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($_POST["morada"] == "") {
echo "Por favor preencha os dados em branco sobre a entidade<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($_POST["codigo"] == "") {
echo "Por favor preencha os dados em branco sobre a entidade<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($_POST["localidade"] == "") {
echo "Por favor preencha os dados em branco sobre a entidade<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($_POST["telefone"] == "") {
echo "Por favor preencha os dados em branco sobre a entidade<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($_POST["fax"] == "") {
echo "Por favor preencha os dados em branco sobre a entidade<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($_POST["cont"] == "") {
echo "Por favor preencha os dados em branco sobre a entidade<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($_POST["contacto"] == "") {
echo "Por favor preencha os dados em branco sobre a entidade<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($_POST["nome1"] == "") {
echo "Por favor preencha os dados pessoais em branco1<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($_POST["email1"] == "") {
echo "Por favor preencha os dados pessoais em branco1<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($_POST["cargo1"] == "") {
echo "Por favor preencha os dados pessoais em branco1<br>
<a href='javascript: history.go(-1)'>voltar</a>";

} elseif
($t2 != 1) {
echo "Por favor preencha os dados pessoais em branco<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($t3 != 1) {
echo "Por favor preencha os dados pessoais em branco<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($t4 != 1) {
echo "Por favor preencha os dados pessoais em branco<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($_POST["nome1"] != "" && $_POST["jantar1"] == "") {
echo "Por favor se pretende ou nao ir ao jantar<br>
<a href='javascript: history.go(-1)'>voltar</a>";
}
elseif
($_POST["nome2"] != "" && $_POST["jantar2"] == "") {
echo "Por favor se pretende ou nao ir ao jantar<br>
<a href='javascript: history.go(-1)'>voltar</a>";
}
elseif
($_POST["nome3"] != "" && $_POST["jantar3"] == "") {
echo "Por favor se pretende ou nao ir ao jantar<br>
<a href='javascript: history.go(-1)'>voltar</a>";
}
elseif
($_POST["nome4"] != "" && $_POST["jantar4"] == "") {
echo "Por favor se pretende ou nao ir ao jantar<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($_POST["nome3"] != "" && $_POST["radiobutton"] == "") {
echo "Por favor escolha a forma de pagamento<br>
<a href='javascript: history.go(-1)'>voltar</a>";
} elseif
($_POST["nome4"] != "" && $_POST["radiobutton"] == "") {
echo "Por favor escolha a forma de pagamento<br>
<a href='javascript: history.go(-1)'>voltar</a>";
}
/*
} elseif
($t22 != 1) {
echo "Por favor preencha todos os dados de facturação<br>
<a href='javascript: history.go(-1)'>voltar</a>";
}
*/
else {

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: " . $_POST['empresa'] . " <" . $_POST['email'] . ">\r\n";
$headers .= "<" . $_POST["email"] .">\r\n";
$headers .= "Reply-To: " . $_POST["email"];

$message = "Inscrição edi2008\r\n";
$message.= "
sent
";


// once the variables have been defined, they can be included
// in the mail function call which will send you an email
mail($sendTo, $subject, $message, $headers);
echo "obrigado pela inscricao";
}
?>

magicyte
10-01-2008, 01:06 AM
Well, you can't send e-mails in JavaScript. That is one comment of mine. It would be possible to convert this to JavaScript, but you'd need to use SOME PHP. Plus, it wouldn't make sense to change it into JavaScript. (unless you have a good reason)

My number one concern with this script is that you can't send e-mails in JavaScript.

-magicyte

Nile
10-01-2008, 01:12 AM
Magic is correct, you can't send emails in js. But you also can't send things in POST like you can in PHP, or other server side languages. You can use GET data though. I'd suggest cgi email, if you really CAN'T use PHP. If your host doesn't support PHP I'm sure there are some programs out there that have forms for you to fill out, and you can get your own email form. (That uses there website to send the email ofcourse)

alapimba
10-01-2008, 08:26 AM
well i can use php and the script is working fine BUT i need to make the script with alerts and not jumping to other page as it is now, i thought that for that question i needed to convert everything to javascript :cool:

Strangeplant
10-01-2008, 12:45 PM
Use php and forget the javascript. Validate your form on reentry into the php page and set a flag or variable, and write your messages to a div in the page just for this purpose using the variable to select what to write. If you have many different messages, you can use an array and the error as the index. A simple example:
<? echo('<div id="status_msg" class="status_msg">);
if($err == 1) {
echo('<span style=" color: blue;">Your Request has been Submitted</span>'."\n");
}
elseif($err == 2) {
echo('<span style=" color: red;">Invalid format in field ['.$db_flag.'] Correct and reenter</span>'."\n");
}
else{
echo('Press Submit to Enter a New Request'."\n");
}
echo('</div>');
?>