Skip to content

Instantly share code, notes, and snippets.

@thefrosty
Created October 4, 2011 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefrosty/1263104 to your computer and use it in GitHub Desktop.
Save thefrosty/1263104 to your computer and use it in GitHub Desktop.
ajax response
function send2() {
/* Verify the nonce */
//check_ajax_referer( 'sms-nonce' );
/* Get post data */
if ( !isset( $_POST['ajax_form_data'] ) ) die("-1");
parse_str( $_POST['ajax_form_data'], $form_data );
/* Get the form fields */
$to = sanitize_text_field( $form_data['smsTo'] );
$from = sanitize_text_field( $form_data['smsFrom'] );
$subject = sanitize_text_field( $form_data['smsSubject'] );
$message = sanitize_text_field( $form_data['smsMessage'] );
$error_response = $success_response = new WP_Ajax_Response();
$errors = new WP_Error();
/* Check required fields */
if ( empty( $to ) )
$errors->add( 'smsTto', 'Please enter a user(s) email.' );
if ( empty( $from ) )
$errors->add( 'smsFrom', 'You must enter a from address.' );
if ( empty( $subject ) )
$errors->add( 'smsSubject', 'Please enter a subject line.' );
if ( empty( $message ) )
$errors->add( 'smsMessage', 'Please enter a message.' );
/* If required fields aren't filled out, send response */
if ( count ( $errors->get_error_codes() ) > 0 ) {
$error_response->add(array(
'what' => 'errors',
'id' => $errors
));
$error_response->send();
exit;
}
/* Do e-mail address validation */
if ( !is_email( $from ) ) {
$errors->add( 'smsFrom', 'E-mail address is invalid.' );
}
/* If any further errors, send response */
if ( count ( $errors->get_error_codes() ) > 0 ) {
$error_response->add(array(
'what' => 'errors',
'id' => $errors
));
$error_response->send();
exit;
}
$message = wordwrap( $message, 150 );
$headers = 'From: ' . $_POST['from'] . "\r\n" .
'Reply-To: ' . $_POST['from'] . "\r\n" .
'X-Mailer: PHP/';
wp_mail( sanitize_email( $to ), $subject, $message, $headers );
//Send back a response
$success_response->add(array(
'what' => 'object',
'data' => 'Message send successful.'
));
$success_response->send();
exit;
}
@thefrosty
Copy link
Author

a is undefined
[Break On This Error] (function(a,b){function cu(a){return f...oveEvent(a,h,s.handle),g=null,delete
jquery...r=1.6.4 (line 2)

@ericmann
Copy link

ericmann commented Oct 4, 2011

If you can, switch to the unminified version of jQuery for development ... that will give you better error messages.

@thefrosty
Copy link
Author

I have SCRIPT_DEBUG defined.. Is there an unminifed definition?

@thefrosty
Copy link
Author

object is undefined
[Break On This Error] length = object.length,
jquery.js?ver=1 (line 632)

@ericmann
Copy link

ericmann commented Oct 4, 2011

I don't think WP comes with an unminified jQuery ... you might need to enqueue your own: http://code.jquery.com/jquery-1.6.4.js

@ericmann
Copy link

ericmann commented Oct 4, 2011

OK, I see that error hold on ...

@ericmann
Copy link

ericmann commented Oct 4, 2011

In your JavaScript, you use $.each a couple of times. When you use .each(), jQuery passes a reference to the collection you're iterating over into the function as the variable object. But $.each() isn't defining a collection ... so object won't be defined.

@ericmann
Copy link

ericmann commented Oct 4, 2011

OK, scratch that ... just haven't see $.each() before ... but that's likely where your problem is.

@ericmann
Copy link

ericmann commented Oct 4, 2011

If you can, set a breakpoint there in Firebug so you can step through and see what value the AJAX call is returning. I suspect that res.responses or res.errors are undefined and that's where your error's coming from.

@thefrosty
Copy link
Author

Yeah, looks it's not defined. So I am assuming the function isn;t working proper..

@ericmann
Copy link

ericmann commented Oct 4, 2011

The entire res object is undefined, or res.responses? If it's res itself, I'd double check the wpAjax.parseAjaxResponse function that's creating it. I usually work with data sent directly back and forth, so I can't help you with that method ...

@thefrosty
Copy link
Author

this.data[0] is undefined
[Break On This Error] if ( this.data[0].firstChild == t...deType != 1 ) { // scalar error data
edit.p...age=sms (line 80)

which is now a wpAjax.parseAjaxResponse issues..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment