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.
Related
This is an odd situation and my current thought is that it doesn't work this way, but I need to some other eyes on this.
A different website I don't have control over has a form with a hidden field in it. The form action is a POST and to send it to a url on my website and I need to be able to get the value of that hidden field using javascript.
As a GET that would be included in the url and I think I would just be parsing that apart. But since it's a POST being sent to me I'm not entirely sure how to get the value of that hidden field out.
Is this doable? If so, where should I be looking to do it?
Thank you!
If your server that is receiving the sended form data uses PHP, you can get all form values using:
<?php
print_r($_POST);
?>
If the page in your server is a static html page, then you cannot get the POST data. Or you can, but then you have to make html pages to be executed as php pages (not recommended however).
You talk about that you need this value be accessible by javascript. Simply do something like:
<script>
<?php
echo 'var input_field_value="'.htmlspecialchars($_POST['name_of_input_field']).'";';
?>
</script>
The question doesn't provide information what server software is used, so I assume that is PHP.
EDIT: after Saturnix's comment I added a call to htmlspecialchars() to make it safe to execute in javascript.
--edit--
(Trying to rephrase my question to be less confusing..)
My understanding is that you should validate forms both client and server side in case Javascript is not working.
How do advanced developers accomplish this in a seamless interface (if one had to describe in a general manner)?
Currently I have Javascript validating my input fields on the fly with 'onkeyup ()', highlighting invalid fields.
If I run the same validations with PHP after the user submits, then I have to redirect to the form if there were any errors, refreshing the page.
I thought this method was lacking sophistication.
What would be the best method?
Thanks in advance.
--- original question below ---
So I created a form with client side javascript to validate the input as the user types. It highlights the boxes w red borders if the data is invalid (using javascript to alter the css). I would like to re-validate the same data with php server side in case of any problems with javascript client side.
I am trying to figure out what is the proper (or best) way to accomplish this.
Currently the form action is setup to go to "register_post.php" after user hits submit.
--
So do I just validate the form data in PHP in "register_post.php", and redirect back to the form page if something is invalid, or is there a more sophisticated way to do this?
One annoying result of this is the page refreshing when the page is redirected.
Is there a more sophisticated way to do this?
--
Another related question is - should I prep my code for javascript not working at all? ..since currently, I use javascript to highlight the fields if the data is invalid. The user will have no indication of which fields are invalid without js.
please bear with me as I am a beginner.
You are trying to valid the post data with PHP and without refresh the page, I don't know if I get your point right. If you want to valid the data with PHP and without refreshing page, you could use AJAX. You can use javascript to post data to a PHP script and deal PHP return data.
An example of jquery ajax method :
<script>
var validDataArr = ''; //data need to be valided
$.ajax({
type:"post",url:"http://"+top.location.hostname+"/valid.php",
data:validDataArr,
success:function(context){alert(context);}
});
</script>
While submitting the form , call a javascript function to validate the fields and throw an error if it not suits.
you can call a js function as below
input type="submit" name="Submit" id="button2" value="Submit" onClick="javascript:verify()"/>
Try this , this will help you to understand the basic validation
http://jsfiddle.net/NWWL4/15/
I am using partial views and ajax based functionality to POST to my controller. I want to know how I can leverage the validation functionality I normally get with the MVC Razor forms and using a full page post back with a async based framework.
Pseudo Code
function buttonpress()
{
if(Validate())
.... Call server functionality with AJAX and handle result
else
.... Show standard validation messages as normal. Do not contact server
}
function Validate()
{
... Check the validation like it would with full post back
... Should also limit to a group too
}
I don't want to rewrite the all the free validation I get from the framework if I don't have to.
I am working on a basic HTML page that requires the user to send details to a script located on a third-party website. What I require is for the user to fill out a form on my web page, and have that information submitted to another third-party form.
I do not wish to return anything to the user, other than whether the submission was successful or not. I also do not want the user to have to go to this third-party site to submit using their form.
It was suggested by the website itself to use an iframe and hold its form on your page, but I was wondering what other, preferably better methods are available to me. It'd be nice if there were some form of jQuery/js code I could use to do such a thing.
It'd be nice if there were some form
of jQuery/js code I could use to do
such a thing.
One way is to use jQuery's $.ajax or $.post methods like this:
$.ajax({
url: url,
success: function(data) {
alert('succeeded');
}
});
Maybe you could try cURL with CURLOPT_POST and CURLOPT_POSTFIELDS?
well it depends if you have control over the other website as well. as in you are able to access the code.
If you are you can use JSONP to pass the values and get a response, but to do it you will have to assign a callback that is sent and then formatted at the front of a JSON object for it to work (they do this for security).
The other option is to use a php ob_start() function. (Note: this will only work if the form you are trying to submit these values to allow $_GET to be used to proccess the form)
ob_start();
include('http://wwww.anotherwebsite.com?key1=value1&key2=value2&key3=value3');
$returnString = ob_get_contents();
ob_end_clean();
So then from here $returnString is the result, which you can basically search (strpos() to see if true is how I would do it) in php to find key words to see if it was successful or not or what ever you need to check for.
But again, this only works if the form on the remote server uses $_GET and not $_POST (or allows both through globals).
I know this is a php solution, but a warning is that for security purposes, there are serious restrictions on what javascript can do cross server.. the best javascript way to do cross server is JSONP, which jQuery does support so you might want to look into that.. but as I mentioned, for it to work you need to have a callback be able to be sent back with the response, and the response needs to be in a jsonp object format.. (basically you either need to 1. have the other server have a jsonp api for you to use or you have control over the other server's server side files to make the changes needed).
Do you want like that? It's simple form submitting to another website. But, I can't check whether it's successfully submitted or not.
<form action="http://www.another.com">
<input name="myInput" type="text">
<input type="submit" value="Submit">
</form>
I wanted to know how to send e-mail using javascript.
I dont want to use long functions with tag n all other stuff. Interested in only one/two liner statement which will allow me to send mail.
I have used something like that earlier :
function sendmail(_frm)
{
var eml="you#youraddress.com";
var bod="&body="+_frm.selOne.value+" ¦¦ "+_frm.txtOne.value;
var subj="?subject=Whatever you want";
location.href="mailto:"+eml+subj+bod;
}
At Form tag
<form action="mailto:you#youraddress.com"
enctype="text/plain"
method="POST" onsubmit="sendmail(this);return false;">
I dont want to use above approach to send mail...
Please provide me your suggestion so that i can send mail very easily by using javascript , like below.
e.g.
function sendmail () {
location.href="mailto:<other stuff>"
}
Is anyone has any idea about this, please share their ideas here.
Thanks a lot....
As far as I know there is no other way to send an e-mail from client side with javascript. You can write some server side code or you can find some service to send e-mail. Unfortunaltely I dont know any web application that gives that kind of service. But, I was writing a web application that have limited e-mail functionality added. It is not finished yet (but still usable).Address is http://postdatabase.appspot.com
Like I said it is not finished yet, that's why I suggest you to find a completed product. if you decide to use it please contact me from the site, so I can be more carruful to make changes.