Jump to content

Recommended Posts

im creating a form and i want to use javascript to validate the input by checking it against regex's  and displaying error messages but i want to processes and use the $_POST function to transfer the info...how do i go about enbedding a  javascript function in my php document. i tried to just enter the script and save it as a php file but nothing happenned when i clicked the submit button...

 

heres the javascript

<script type = "text/javascript">
      function validate(){
        //get inputs
        title = document.getElementById("txtTitle").value;
        description = document.getElementById("txtDescription").value;
        location = document.getElementById("txtLocation").value;
      
        //console.log("Title: " + title + ", Description: " + description + ",  Location: " + Location);

        // title must be at least ten characters
        if (title.match({10,80}) == null){
          alert("Please enter a more descriptive Title.");
        } else {
          //name is OK, now check email
      
          //description must exist
          if (description == ""){
            alert("Please enter a Description.");
          } else {
  
            //description is OK, now check local
           
  
             if (location == ""){
            alert("Please enter a Location.");
          } else {
              
            } // end location if
          } // end description if
        } // end title if
      } // end function

    </script>

 

and heres the button

button type = "button"
                onclick = "validate()">
          submit
        </button>

Link to comment
https://forums.phpfreaks.com/topic/141872-javascript-in-php/
Share on other sites

php is parsed on the server.  javascript is parsed on the client.  php looks at javascript as regular text to be output to the client.  It doesn't care about it.  You can use javascript to validate form fields and then have the info submitted by traditional methods (just clicking the form's send button, with the form's method set to 'post'), or you can use some ajax to send it to the server. 

 

Not that I would recommend clientside validation anyways.  It is a trivial thing, even by script kiddie standards, to get around clientside validation. 

Link to comment
https://forums.phpfreaks.com/topic/141872-javascript-in-php/#findComment-742815
Share on other sites

well im using this and it should work, but im getting an error on the "{" right under the first if statement....??? what am i doing wrong?

if ( !ereg(".{10,80}",$_title)
    {
     print ("<script language = 'javascript'>alert('Please enter a more descriptive Title.');</script>");
    }

if ( $_description = "")
    {
     print("<script language = 'javascript'>alert('Please enter a Description.');</script>");
    }

if ( $_location = "")
    {
     print("<script language = 'javascript'>alert('Please enter a Location.');</script>");
    }	

Link to comment
https://forums.phpfreaks.com/topic/141872-javascript-in-php/#findComment-742817
Share on other sites

ok well how how do i get a button to run a function..display the errors before actually doing the form action. heres what i got now

 

<?php

function errors() {

if ( !ereg(".{10,80}",$_title) )
    {
     print ("<script language = 'javascript'>alert('Please enter a more descriptive Title.');</script>");
    }

if ( $_description = "")
    {
     print("<script language = 'javascript'>alert('Please enter a Description.');</script>");
    }

if ( $_location = "")
    {
     print("<script language = 'javascript'>alert('Please enter a Location.');</script>");
    }	
}

?>

 

i don't think i have the button right

<input type="submit" name="do" value="Submit" onclick = "errors()" >

Link to comment
https://forums.phpfreaks.com/topic/141872-javascript-in-php/#findComment-742835
Share on other sites

Like I said, you can't just intermingle php and javascript like that.  PHP parses and sends results to the client.  If you want server/client interaction without using a submit button,  you would have to separate them and use ajax to make a request to a script and call functions based on what you send via ajax methods.

Link to comment
https://forums.phpfreaks.com/topic/141872-javascript-in-php/#findComment-742837
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.