Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.

Author Topic: two forms using the same mail.php  (Read 1919 times)

kreatur

  • Jr. Member
  • **
  • Karma: 0
  • Posts: 73
    • RoManga
two forms using the same mail.php
« on: May 26, 2010, 03:11:28 PM »

I have two forms that use onChange from a select box to replace one another. Problem is, only the first one sends mail with data, the second one doesn't seem to output the content of $name, $subject, etc. even though the fields are identical except one i added on the second form. This is when i remove the validation i have on the javascript side, cause with validation it echoes the errors.

I could post the code here, but it's kind of long, i've upped the work-in-progress script over here: http://romanga.5com.ro/form.zip in case any of you has time to look over it.

Thank you in advance.
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: two forms using the same mail.php
« Reply #1 on: May 26, 2010, 04:34:49 PM »

sNews uses the // SEND EMAIL function to compile and send the content as an e-mail.
Search for it in your snews.php file... it starts around line 3603 in the file you posted a couple of days ago on the categories problem.

When you search your file for the function-name - send_email($send_array); - you'll find it located (being used) in both the Comment and Contact functions.

Whatever you have added as an extra field to either the Comment or Contact form functions, the value for that field will also need to be added to the // SEND MAIL function. The same would apply to any form function that uses the // SEND EMAIL function.

To be safe... you could leave the // SEND EMAIL function as it is, take a copy of it and paste in in below the default one, change the function-name to  send_email2($send_array) to make it unique from the other one, and make the same change where it is being called within your form function. Then make the modifications required to your new send_email2 function till you get it working.

Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

kreatur

  • Jr. Member
  • **
  • Karma: 0
  • Posts: 73
    • RoManga
Re: two forms using the same mail.php
« Reply #2 on: May 26, 2010, 05:48:31 PM »

You misunderstood, the form is not for snews, that's why i posted on Web Programming.
And my problem is that i have a form with two divs that change onChange. I have the same fields in both (so the same ids and names), but i only get output if the fields from the first one are filled. The second div errors on the javascript validation and doesn't retrieve the values from the variables. Even if give different names to those fields, it will still error because the php also tries to output the first div's variables, which will be empty.

I believe i could do separate forms and validation for each div, but it would be better to keep them all in one place if possible.
Logged

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: two forms using the same mail.php
« Reply #3 on: May 27, 2010, 02:21:38 AM »

Why not try OOP?  I know it is not the solution to the code you already have, but you can give it a try if you don't know it as it could be a solution for future scripts/work.

I did an *VERY QUICK AND UNTESTED SAMPLE* example script here  - I wouldn't use it for production use or anything really, but it gives an idea of how it could be done:

Code: (UNTESTED CODE) [Select]
<?php

class Mail {

var $form// Name of the form - private
var $val = array(); // Validation field array - private

var $to // Defined somewhere...
var $subject // Defined somewhere...
var $message; // Defined somewhere...

function __construct$form_name ) {
$this->form $form_name;
}

function add_validation$field$callback ) {
$this->val[] = array('field' => $field'callback' => $callback);
}

function validate() {
if ( isset($_POST[$this->form .'_submit']) ) {
$false_field = array();
$validated 'TRUE';

foreach ( $this->val as $v ) {
// Expecting a boolean return
$fields_are $v['callback']( 
$_POST[$this->form .'_'$v['field']] 
);
if ($fields_are == 'FALSE') {
$validated 'FALSE';
$false_field[] = $this->form .'_'.$v['field'];
}
}

if( $_POST[$this->form .'_message'] == '' ) {
$validated 'FALSE';
$false_field[] = $this->form .'_message';
}

 
if ($validated == 'FALSE') {
$validated implode(', '$false_field) .' are '$validated;
} else {
$validated 'All good to send!';
}
return $validated;
}
}

function send() {
echo $this->validate();
/*
mail(
$this->to, 
$this->subject, 
$this->message
);
*/
}
}

function 
check_name($name) {
$isvalid 'TRUE';
if (empty($name) || $name == '') {
$isvalid 'FALSE';
}
return $isvalid;
}

function 
check_subject($subject) {
$isvalid 'TRUE';
if (empty($subject) || $subject == '') {
$isvalid 'FALSE';
}
return $isvalid;
}

$form_one = new Mail"form_one" );
$form_one->add_validation('name''check_name');
$form_one->add_validation('subject''check_subject');
$form_one->send();

$form_two = new Mail"form_two" );
$form_two->add_validation('name''check_name');
$form_two->send();

?>

<form name="form_one" method="post" action="">
<p>
<label>Name:</label>
<input type="text" name="form_one_name" value="<?php echo $_POST['form_one_name']; ?>" />
</p>
<p>
<label>Subject:</label>
<input type="text" name="form_one_subject" value="<?php echo $_POST['form_one_subject']; ?>" />
</p>
<p>
<label>Message:</label>
<textarea name="form_one_message" value="<?php echo $_POST['form_one_message']; ?>"></textarea>
</p>
<p>
<input type="submit" name="form_one_submit" />
</p>
</form>

<form name="form_two" method="post" action="">
<p>
<label>Name:</label>
<input type="text" name="form_two_name" value="<?php echo $_POST['form_two_name']; ?>" />
</p>
<p>
<label>Message:</label>
<textarea name="form_two_message" value="<?php echo $_POST['form_two_message']; ?>"></textarea>
</p>
<p>
<input type="submit" name="form_two_submit" />
</p>
</form>
<?php print_r($_POST); ?>

Pure JS + some Jquery would be:
(Another *VERY QUICK AND UNTESTED SAMPLE* I DO NOT RECOMMEND THIS - it is only for proof of concept as above PHP code)
Code: [Select]
<script type="text/javascript" src="../../lib/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
function callFrmFunc( func, param ) {
// Call the form object
// Eval is bad, yes I know...
eval( func + '(' + param + ')');
}

function checkMailForm( form_name ) {
// Properties
this.formname = form_name; // Name of form
this.val = []; // Array of validation

// Methods
this.add_validation = function(field, call) {
var valPair = '{ "field" : "' + field + '", "callback" : "' + call + '" }';
this.val.push( valPair );
}
this.validate = function() {
var frm = this.formname.name;
var stmnt = 'TRUE';
var falseFields = [];
for (var i in this.val) {
var val = $.parseJSON( this.val[i] );
// ie form_one_name or form_one_subject
var frmField = frm + "_" + val.field;
// Getting the value of the input field:
// callbackFunc($('input[name="frmField"]').val());
// Eval is bad, I know,  I know...
stmnt = eval( val.callback + "( $('input[name=\"" + frmField + "\"]').val() )" );
if (stmnt == 'FALSE') {
falseFields.push(val.field);
}
}
if (falseFields != '') {
stmnt = 'FALSE';
}
return stmnt;
}
this.send = function() {
var stmnt = this.validate();
if (stmnt == 'TRUE') {
alert('Waiting to be sent...');
} else {
alert('Nothing is being sent!');
}
// Do the ajax request
}
}

function check_name(name) {
var isvalid = 'TRUE';
if (name == '' || typeof(name) == "undefined") {
isvalid = 'FALSE';
}
alert('check_name is ' + isvalid);
return isvalid;
}

function check_subject(subject) {
var isvalid = 'TRUE';
if (subject == '' || typeof(subject) == "undefined") {
isvalid = 'FALSE';
}
alert('check_subject is ' + isvalid);
return isvalid;
}
</script>
<form name="form_one" method="post" action="">
<p>
<label>Name:</label>
<input type="text" name="form_one_name" value="" />
</p>
<p>
<label>Subject:</label>
<input type="text" name="form_one_subject" value="" />
</p>
<p>
<label>Message:</label>
<textarea name="form_one_message" value=""></textarea>
</p>
<p>
<input type="submit" name="form_one_submit" id="form_one_submit" />
</p>
</form>

<form name="form_two" method="post" action="">
<p>
<label>Name:</label>
<input type="text" name="form_two_name" value="" />
</p>
<p>
<label>Message:</label>
<textarea name="form_two_message" value=""></textarea>
</p>
<p>
<input type="submit" name="form_two_submit" id="form_two_submit" />
</p>
</form>
<script type="text/javascript">
// User would enter this for each form
function form_one_check( formName ) {
var mailForm = new checkMailForm(formName);
mailForm.add_validation( 'name', 'check_name' );
mailForm.add_validation( 'subject', 'check_subject' );
mailForm.send();
}

function form_two_check( formName ) {
var mailForm = new checkMailForm(formName);
mailForm.add_validation( 'name', 'check_name' );
mailForm.send();
}

// OnSubmit...
$(document).ready(function(){
$('form').submit(function() {
callFrmFunc( this.name + '_check', this.name );
});
});
</script>
« Last Edit: May 28, 2010, 01:48:58 PM by nukpana »
Logged