0

I have a new problem. My whole website is written in PHP as well as all validations. Is there a way to do validations in php and then execute javascript like the example bellow?

if (@$_POST['submit']) {
    if ($txt == "") {
        $err = "No comment";
    } 
    else {
        echo "<script type='text/javascript'>
            function myFunction() {
                var txt' = '$txt';
                var dataString = 'txt=' + txt;
                $.ajax({
                  type: 'POST',
                  url: 'ajaxjs.php',
                  data: dataString,
                  cache: false,
                  success: function(php) {
                      alert(php);
                  }
               });
            }
        </script>";
    }
}

<div id="text">
    <form action="" method='POST'>
        <textarea maxlength="2000"></textarea>
        <input type='button' onclick="myFunction()" name='submit' value='post' />
    </form>
</div>

This doesn't work. So I'm wondering how should I do it? I guess forms don't work with javascript, but how do I do it without a form?

6
  • 1
    What exactly are you expecting as output? You don't use anywhere your javascript function.myFunction Commented Jul 16, 2016 at 20:13
  • @PeterDarmis Sorry, I posted the wrong core. Updated now. It doesn't work with onclick="myFunction()" either Commented Jul 16, 2016 at 20:17
  • 1
    @Menel if you're trying to execute a function onsubmit then you might use <form onsubmit="formSubmit();"> and make formSubmit() return false, this is important. Commented Jul 16, 2016 at 20:19
  • @Menel you don't need to use php here only jQuery/Javascript is enough Commented Jul 16, 2016 at 20:28
  • @timmyRS that sounds like something that could solve this for me. How do I make formSubmit() return false? Do I do it at the end of validation in the javascript or? Commented Jul 16, 2016 at 20:36

3 Answers 3

2

You don't need to use php at all. You can post your textarea data like in the below example.

HTML

<div id="text">
<textarea id="txtArea" maxlength="2000"></textarea>
<button id="btnSubmit" name='submit'>post</button>
</div>

Javascript/jQuery

$("#btnSubmit").on('click',function(e) {
   e.preventDefault();
   var txtValue = $("#txtArea").val();
   if(txtValue.length==0) {
     alert("You have not entered any comments");
   } else {
     $.ajax({
        type: 'POST',
        url: 'ajaxjs.php',
        data: {txt:txtValue},
        cache: false
        })
  .done(function() {
    alert( "success" );
  })
  .fail(function() {
    alert( "error" );
  });    
  }
});
Sign up to request clarification or add additional context in comments.

Comments

1

The solutions is: 1. add function for submit event. 2. call ajax with form fields values as data. 3. do vildation inside php called with ajax request and return status code (valid/not valid) 4. analyse code in js and output error/success message.

Comments

1

First of all: Your code has a couple of errors.

  • You are asking if $txt == "" whilst $txt was not visibly set.

  • Your text area has no name

  • Your if doesn't ask if empty($_POST["submit"])

Second of all: You mentioned that you want the code to be executed on submit of the form. Therefore you can simple do this:

<form onsubmit="formSubmit();">
    ...
</form>
<script>
function formSubmit()
{
    if(...)
    {
        return true; // Valid inputs, submit.
    }
    return false; // Invalid inputs, don't submit.
}
</script>

The return false is important because if it would miss, the form would be submitted as usual.

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.