0

I have a live preview form: http://davidwalsh.name/jquery-comment-preview Which uses jquery to display the preview in real time:

$(document).ready(function() {
  $('#live-preview-form input, #live-preview-form textarea').bind('blur keyup',function() {
    $('#lp-comment').text($('#comment').val());
    $('#lp-comment').html($('#lp-comment').html().replace(/\n/g,'<br />'));
  });
});

what if i need to apply some PHP function on "#comment" which comes from textarea, before putting it in the jquery validate?

1
  • put jQuery validation code on form submit Commented Apr 13, 2011 at 6:12

2 Answers 2

1

If you want to apply PHP functions, you must use jquery ajax(POST the value to a php page and get the returned result).

For example:

process.php:

<?php
function my_function($in){
    ....
    return $out;
}

$result = my_function($_POST['q']);
echo json_encode($result);
?>

js:

$(document).ready(function() {
  $('#live-preview-form input, #live-preview-form textarea').bind('blur keyup',function() {

    $.ajax({type: "POST", url: "function.php", data:{ "q": $('#comment').val()}, dataType: "json", 
      success: function(result){
        $('#lp-comment').text(result);
        $('#lp-comment').html($('#lp-comment').html().replace(/\n/g,'<br />'));
      }});
  });
});

However, this is not recommended because users query your server every time when they type a single character. The best way is to by a javascript function instead of a PHP function.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you should use the jquery form validation plugin available here. It deals with all the basic form validation you need .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.