PHP form client side validation [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to determine if user selected a file for file upload?
I would like a simple PHP form validation. My form has two inputs: A link and a file to upload. The form should be submitted only if the link field is filled and the user selected a file to upload. If one of them is or both are false, an alert box should be displayed (like here when you click Upload without selecting a file).
This is my form:
<form action="upload_file.php" enctype="multipart/form-data" method="post">
<p>Link:<br>
<input type="text" name="link" size="50">
</p>
<p>Image:<br>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input type="file" name="file" id="file" size="40">
</p>
<div>
<input type="submit" name="upload" value="Upload">
</div>
</form>
I am new to PHP and I have been browsing tutorials for hours now, many of them was of no help or shows the server side validation.
I understand I should start with this:
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
to validate the fields before the form is submitted. I have some basic knowledge of PHP but I cannot figure out how to do this easy or not so easy task.

php is server side. What you need is a client side checker (js, jq ..)
You can perform the check only with php, but on your server, then return to the client the error message or the task complete message. If you want the check to be done client side, you need a client side language to handle it.

PHP is indeed a server-side application and thus cannot perform client-side validation.
If you really want to do client-side validation, you'll probably have to use javascript. Have a look at the jQuery validation plugin. Here is a big demo page showing you some of the possibilities: http://jquery.bassistance.de/validate/demo/
For more help and information, also have a look at the jQuery.com/plugins/validation page.
(Note the WARNING at the end of this post)
Example:
(the javascript)
<script>
$(document).ready(function(){
$("#commentForm").validate();
});
</script>
(the form)
<form class="cmxform" id="commentForm" method="get" action="">
<fieldset>
<legend>A simple comment form with submit validation and default messages</legend>
<p>
<label for="cname">Name</label>
<em>*</em><input id="cname" name="name" size="25" class="required" minlength="2" />
</p>
<p>
<label for="cemail">E-Mail</label>
<em>*</em><input id="cemail" name="email" size="25" class="required email" />
</p>
<p>
<label for="curl">URL</label>
<em> </em><input id="curl" name="url" size="25" class="url" value="" />
</p>
<p>
<label for="ccomment">Your comment</label>
<em>*</em><textarea id="ccomment" name="comment" cols="22" class="required"></textarea>
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
</fieldset>
</form>
WARNING
Please keep in mind that javascript, being client-side, runs on the client's computer. Don't rely on javascript validation alone. Anyone can disable javascript and insert 'wrong' values in your form.

In case of form it's best to do client side validation using some javascript library like jQuery. Check out this jQuery tool for form validation.
For server side validation, check out this link. It may help you.

Validation should be done on server side ALWAYS. You can add client minor validation also as a confort for the user with javascript.
Here some basic code to show a message if fields empty using minor validation:
<input type="submit" name="upload" value="Upload" onclick="var ref = document.getElementById('file'); if ( ref.length == 0 ){ alert( 'select file' ); return false; } ref = document.getElementById( 'textfieldid' ); if ( ref.value.length == 0 ){ alert( 'fill link, better use a regexp to match a url but this another store' ); return false; }">

For client side validation of "File upload" field, refer following plug-in :
http://adamsanderson.github.com/jQuery-File-Validator/
It is useful. I have tried it.

Related

how to get in url with value.html

This form submits url like that
http://www.website.com/page/search.php?search=demo&submit=Search
<form action="search.php?search=" method="GET" id="search-form">
<input id="search-text" name="search" type="text" maxlength="30" placeholder="type keyword"/>
<button type="submit" name="submit" value="Search" id="search-button">Search</button>
</form>
Where I want to post the form like that:
http://www.website.com/page/search/demo.html
How can I do this?
I know it is a small thing but help me..
Thanks
I am unclear what your question is.
If you mean that you want to be able to process the input client-side, that is not possible unless you override the behavior with javascript. For example with jQuery
$( "search-form" ).on( "submit", function( event ) {
// Do stuff here with the form data
event.preventDefault();
});
If you mean that you don't want .php to show up and want to hide the underlying server technology, then you can use something like Apache's mod_rewrite. But you probably don't want that in this case because .html implies that it's a page you visit (GET, not POST) rather than a script you submit variables to.
Save this code as form.html on your desktop then open it with your browser then add some values and click the button. Look at the url afterwards
<form method="get" action="#">
<input type="text" name="input1">
<input type="text" name="input2">
<input type="text" name="input3">
<input type="text" name="input4">
<button type="submit">Click me and look at the url</button>
</form>
After seeing your edited question, try this with the above steps( save on the desktop etc.etc)
<form method="post" onsubmit="this.action=window.location.href.replace('.html',inputurl )">
<input type="text" name="input1" onblur="inputurl='/'+this.value+'.html'">
<button type="submit">Click me and look at the url</button>
</form>

User input not showing in search form

I have a free form search that I moved from my main content to my header. Once I moved the form into the header the text inside of it stopped showing. The placeholder works but when you begin typing it is completely blank. The search and the functionality work perfectly, I just cannot see what I am typing. Any ideas?
<form class="search" action="/all-results/" method="get">
<fieldset>
<span class="text"><input name="searchProducts" value="All" type="hidden">
<input type="hidden" name="search_selection" value="all">
<input name="byString" id="s" type="text" value="" placeholder="<?php echo __('Search','Avada'); ?>" /></span>
</fieldset>
<form name="input" action="/all-results/" method="get">
<input type="submit" id="search_button" value="Search">
</form>
</form>
Your HTML is invalid. You can not nest a <form> element inside another <form>. Messing up forms and tables are two of the best ways to produces peculiar browser behaviour, so my guess is that this is your problem.
If you correct your HTML (validate it!) and the problem persists, then post a minimal HTML page with CSS so that there is something for us to debug.

Facebook iframe - how to send form (POST method)?

I have very simple form (the file is called message.php):
<?php
print_r($_POST);
?>
<form method="post" target="_top" action="<?php echo CANVAS_URL;?>message.php">
<input type="text" name="your_name" />
<input type="hidden" name="signed_request" value="<?php echo $_REQUEST['signed_request'];?>" />
<input type="submit" name="send" />
</form>
I found one solution of this issue - put into the form hidden input with the signed_request - I did it but unfortunately I am still facing with this problem -- I cannot retrieve sent POST data.
If I change the method to method="get", everything is working well, but I would need to data from POST.
Could anyone help me, how to solve this problem? Thanks!
Try this. I don't believe you need to use target in FB canvas aps anymore. Also a form ID would be good.
<form method="POST" id="my_form" action="message.php">
<input type="text" name="your_name" />
<input type="hidden" value="<?php print $_POST["signed_request"] ?>" name="signed_request" />
<input type="submit" name="submit" />
</form>
POSTing to Canvas URLs (as in http://apps.facebook.com/namespace) is simply not supported.
But why post to the top window instead of simply staying within the iframe? It's way better as it doesn't require the entire page to be reloaded, only the iframe.

Form with multiple submit option. PHP

I have a HTML form page with following code :
<form action="chainresult.php" method="post" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="3145728"/>
<input type="file" name="userfile" id="userfile" size="30" />
<input type="submit" value="GET SEQUENCE" />
</form>
<form action="helix_info.php" method="post" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="3145728"/>
<input type="file" name="userfile" id="userfile" size="30" />
<input type="submit" value="GET HELIX INFO" />
</form>
My page has two browse options and two submit options which takes the use to 2 php pages. I want to have only one browse option with two options that takes the user to 2 different php pages based on what the user clicks.
Any help is appreciated!
You will need to combine the two forms into one (you don't even necessarily need the form tags), use JavaScript or jQuery to capture the submit button click, evaluate the input value based on your validation rules that route the form submission, and then post the values to a form, likely through ajax.
You can submit multiple forms but you will have to use Javascript. It should be doable with jQuery without too much sweat and tears. Something like...
$("#my-submit-button").click(function(){
$("#first-form").submit();
$("#second-form").submit();
})
I am not sure I understand your question correctly, but if you want to be able to post data to two different URLs, with two different submit-buttons, having two different forms is the only way to do it with plain HTML.
However, it would be possible to use JavaScript. In that case you could mash both forms together, evaluate the input before sending any information, and the post the data to different URLs depending on the input, using AJAX.
It is worth noting that going down the JavaScript-road, you would make the form unusable for anyone who has disable JavaScript.
I would probably suggest that you make it a single form, point it to an URL that can handle either case. So you always post the data to the same URL, and the server-side code would have to evaluate the input and decide what to do with it. That case you don't eliminate users that doesn't have JavaScript activated.
Not sure if I understood the question properly, but you could use jQuery to change the action attribute of your form, depending on what the user chooses. Something among the lines of:
<form id="myform" action="dummy.php" method="post" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="3145728"/>
<input type="file" name="userfile" id="userfile" size="30" />
<input type="radio" name="formtype" value="uploadscript1.php" /> Option 1<br>
<input type="radio" name="formtype" value="uploadscript2.php" /> Option 2<br>
<input type="submit" value="GET HELIX INFO" />
</form>
And in jQuery:
$('input[name="formtype"]').change(function(){
$('#myform').attr('action', $(this).attr('value'));
});
I am not sure about the jQuery part, but it should work ok. Try experimenting with that. :)
Using this approach you should be able to send the same data to two different forms.

Zend_Form not submitting

I built a custom Zend_Form "myForm" and I passed it to my view with:
$this->view->form=new myForm();
Problem: form is not submitting (page doesn't reload/refresh).I thought something was wrong with the "form" tags,but I copyied the bottom code in another page (that is not a Zend environment) and is working.This is the source code:
<form enctype="multipart/form-data" method="post" action="">
<input type="text" name="title" id="title" value="" class="">
<textarea name="text" id="text" class=""></textarea>
<input type="text" name="allegati" id="allegati" value="" class="">
<input type="hidden" name="MAX_FILE_SIZE" value="2097152" id="MAX_FILE_SIZE">
<input type="file" name="file" id="file" class="media[]"></span>
<input type="submit" name="submit" id="submit" value="submit" class="">
</form>
SOLVED: As some of you guys suggested javascript is giving problems:
I had a js script overriding with:
$('form').submit();
Thanks
Luca
Form submitting issues are 99% related to javascript conflicts with the 'form' element or with a wrong defined 'form' tag.
Always check those above when encountering problems.
P.s. for the remaining 1% feel free to ask at Stack!
Best regards
Just a suggestion - try to rename your submit button to something but not "submit" (i.e name="mysubmitbutton").
I think the problem could be with expandos: http://ejohn.org/blog/deadly-expandos/. By default, a form element has a submit function. But, if you call any field inside your form with the name "submit" (like you do in your example), the form.submit will point to your input element and you'll not be able to submit your form.

Categories