Page 1 of 1
HELP in form processing
Posted: Fri Jun 09, 2006 5:18 am
by mcoelho123
HI guys i have a form that is actions is
Code: Select all
<?php echo $_SERVER['SCRIPT_NAME']; ?>
but i have a variable $send in url
?send=2
how can i do to process the data form in the same page with ?send=n? I tried this
Code: Select all
<?php echo $_SERVER['SCRIPT_NAME'] . "?send=" . $_GET['send'];?>
But dont work.
Thanks in advance
Posted: Fri Jun 09, 2006 5:38 am
by Maugrim_The_Reaper
Are you trying to submit a form, or just pass variables in the url?
You should try to use only one option at a time. An example form that submits to the same page it was loaded from might look like:
Code: Select all
<?php
if(isset($_POST['process_form']) && !empty($_POST['process_form']) && $_POST['process_form'] == 'my_form')
{
// do form processing
// do output and exit
}
// if IF statement fails, the form has not been submitted - echo it from here (uses heredoc syntax)
$form_html = <<<EOF
<html>
<body>
<form action="{$_SERVER[PHP_SELF]}" method="post">
<input type="hidden" name="process_form" value="my_form" />
<input type="text" name="mytexttoprocess" value="" size="32" maxlength="32" />
<input type="submit" name="submit_button" value="Submit Me!" />
</form>
</body>
</html>
EOF;
echo $form_html;
exit(0);
?>
Posted: Fri Jun 09, 2006 5:48 am
by mcoelho123
I have a form to post the data and i have
Code: Select all
if (get_error($from) == true) {
$err1 = "Inserir email";
...
And a variable $to that is to where a mail is send and i use $_GET to get ?send=2,
and when it sends the data or if it haves any error the ?send is lost, and i cant loose it because if i loose it the mail goes nowhere
Posted: Fri Jun 09, 2006 6:00 am
by Maugrim_The_Reaper
Can you post the whole script? It's hard to see what it's doing from isolated snippets...

.
Posted: Fri Jun 09, 2006 6:01 am
by mcoelho123
Code: Select all
<?php
$email[1] = "caselcoop@caselcoop.pt";
$email[2] = "webmaster@caselcoop.pt";
$email[3] = "marco@caselcoop.pt";
$email[4] = "carlos.coradinho@caselcoop.pt";
$email[5] = "anabela.fialho@caselcoop.pt";
$email[6] = "jorge.ramelhete@caselcoop.pt";
$email[7] = "alberto.ferreira@caselcoop.pt";
$email[8] = "anabela.prudencio@caselcoop.pt";
$email[9] = "dulce.lucas@caselcoop.pt";
$email[10] = "joao.traquina@caselcoop.pt";
$to2 = $_POST['to2'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];
function get_error($value) {
if ($value == "") {
return true;
} else {
return false;
}
}
function checkEmail($from) {
return preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/" , $from);
/*return preg_match( "/^
[\d\w\/+!=#|$?%{^&}*`'~-]
[\d\w\/\.+!=#|$?%{^&}*`'~-]*@
[A-Z0-9]
[A-Z0-9.-]{1,61}
[A-Z0-9]\.
[A-Z]{2,6}$/i");
*/
}
function sendmail($to2, $from, $subject, $message) {
$to = $to2;
$message = "<html><head><title></title></head><body>" . nl2br($message) . "</body></html>";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' . $to2 . "\r\n";
$headers .= 'From: NET' . "<" . $from . ">" . "\r\n";
// $headers .= 'Cc: ' . "\r\n";
// $headers .= 'Bcc: ' . "\r\n";
mail($to, $subject, $message, $headers) or die("Email error");
mail("webmaster@caselcoop.pt", $subject, $message, $headers) or die("Email error");
}
function insert_count($to) {
$sql = "SELECT MAX(counts) AS counts FROM email_counter WHERE email='$to'";
$recordset = mysql_query($sql) or die (mysql_error());
$result = mysql_fetch_assoc($recordset) or die (mysql_error());
if (!$result['counts']) {
$sql2 = "INSERT INTO email_counter (email, counts) VALUES ('$to','1')";
$doit = mysql_query($sql2) or die (mysql_error());
} else {
$value = $result['counts']+1;
$sqlInsert = "UPDATE email_counter SET counts='$value' WHERE email='$to'";
$doit = mysql_query($sqlInsert) or die ("Can\'t do It");
}
}
$error_has_ocurred == false;
if (isset($_POST["send_mail"])) {
if (get_error($from) == true) {
$err1 = "Inserir email";
$error_has_occured = true;
}
if (get_error($subject) == true) {
$err2 = "Inserir assunto";
$error_has_occured = true;
}
if (get_error($message) == true) {
$err3 = "Inserir mensagem";
$error_has_occured = true;
}
if ($from != "") {
checkEmail($from);
if (checkEmail($from) == false) {
$err1 = "Email inválido";
$error_has_occured = true;
}
}
if ($error_has_ocurred == false) {
sendmail($to2, $from, $subject, $message);
insert_count($to2);
$info = "Mensagem enviada";
$from = "";
$subject = "";
$message = "";
}
}
?>
<form action='<?php echo $_SERVER['SCRIPT_NAME'] . "?send=2" . $_GET['send'];?>' method="POST" name="Mail Send" id="sendmail">
<li><span class="bodytext11_red">
<?php
if (isset($info)) {
echo "<span class='bodytext11_red'>" . $info . "</span><br><br>";
}
?>
</span><span class="bodytext11">Para: </span><span class="bodytext11">
<?php
$to2 = "";
$default_email = "caselcoop@caselcoop.pt"; // To choose default mail address if none chosen
$default_error = "Email Inválido"; // To display error msg if none chosen
$use_error_msg = 0;
$use_default_mail = 1;
// Choose one of the above methods to use
$email_address = $email[$send];
foreach ($email as $key => $value){
$send2[$key] = $key;
}
if($email_address == "" xor !isset($email_address) xor $send == "" xor !isset($send) xor $send != $send2[$send]) {
if($use_error_msg == "1") {
$to2 = $default_error;
} else if($use_default_mail == "1") {
$to2 = $default_email;
}
// end if email address string is blank
} else {
$to2 = $email_address;
}
echo " " . $to2;
?>
<input name="to2" type="hidden" value="<?php echo $to2; ?>">
</span></li>
<br>
<br>
<li><span class="bodytext11">De (Email): </span><?php echo "<span class='bodytext11_red'>" . @$err1 . "</span>"; ?><br>
<input name="from" type="text" id="from" size="50" value="<?php echo @$from;?>">
</li>
<br>
<br>
<li><span class="bodytext11">Assunto: </span><?php echo "<span class='bodytext11_red'>" . @$err2 . "</span>"; ?><br>
<input name="subject" type="text" id="subject" size="50" value="<?php echo @$subject;?>">
</li>
<br>
<br>
<li><span class="bodytext11">Mensagem: </span><?php echo "<span class='bodytext11_red'>" . @$err3 . "</span>"; ?><br>
<textarea name="message" cols="50" rows="10" id="message" type="text"><?php echo @$message;?></textarea>
</li>
<br>
<br>
<li>
<table width="433" height="36">
<tr>
<td width="139" align="left"><input name="Limpar" type="reset" value="Limpar"></td>
<td width="282" align="right"><input name="Enviar" type="submit" value="Enviar"></td>
</tr>
</table>
</li>
<input type="hidden" name="send_mail">
</form>
Posted: Fri Jun 09, 2006 7:07 am
by mcoelho123
Thank u guys, i used :
Code: Select all
<?php echo $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'];?>
and worked very well