简体   繁体   中英

Validate Form with PHP with Data Posting to Java Application

I have a Java application (servlet, written by someone else) that does some processing. I have a PHP application (web site HTML that uses PHP in places) on top that posts data to the Java application:

<form method="post" action="http://site/java" onsubmit="return validate(this)">

I have the JavaScript validating, as well onsubmit .

This works fine, but when I try and validate with PHP (in case JS is disabled), I run into problems.

I'm not sure how to do this. I've tried a few things but none has been really what I want. I want to be able to mimic the JS behavior but with PHP.

It would be cool if I could do something like this:

if ('POST' == $_SERVER['REQUEST_METHOD']) {
    //do post to servlet
}

I've tried other things like this:

if (isset($_POST['field'])) {
    //validate form with function
}

Part of the problem is that the Java application also does some validation and returns some parameters I can get. I might use something like this to check these:

if ($_GET['error'] == 'invalidEmail') {
    $error = 'Please enter a valid email address.';
}

How would I do this? Can I use Location response header or does this not send POST data? If I set action="" and post back to page, I can get the PHP to validate but obviously the whole point is to post to the Java application.

You can use REST to post to your java application from php. More specifically the cURL library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM