PHP Form without Java

Thread Solved

Join Date: Oct 2009
Posts: 6
Reputation: catcoffee is an unknown quantity at this point 
Solved Threads: 0
catcoffee catcoffee is offline Offline
Newbie Poster

PHP Form without Java

 
0
  #1
Oct 15th, 2009
Hi all!

Сonditions of a problem:

1) in one form post info and upload file
2) validation only php without js
3) associated fields: if the value is not the same, we don't need to validate field
4) associated fields: if the value is the same, we need echo new input

And i don't know how make it all in one form without java.
Google and others resource didn't help me May be you know how or where i can see tutorial for it problem?

I've come to know all w3school, php.net, tizag, tutorials on pixel2life and tutsearch.net, nettuts and even http://www.benjaminkeen.com/software/php_validation/ don't help me.
despair deeply
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 794
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster
 
0
  #2
Oct 15th, 2009
Hi catcoffee and welcome to DaniWeb

1) Can be achieved with a <input type='file'> in a PHP/HTML form. PHP then uses an array, $_POST, to retrieve all posted data and the $_FILE array to access uploaded files.
2) By validation do you mean making sure that the inputted data is acceptable? If so, PHP provides regex functions that can be performed on your $_POST array as well as other ways of validating your form data.
3) and 4) I don't follow what you need here?
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 15
Reputation: webexpertzhere is an unknown quantity at this point 
Solved Threads: 6
webexpertzhere webexpertzhere is offline Offline
Newbie Poster
 
0
  #3
Oct 15th, 2009
Did you have a look at http://www.w3schools.com/PHP/php_file_upload.asp in w3schools site? The code clearly explains all steps required to upload a file using HTML and PHP alone.
What are your doubts in points 3 and 4? Can you elaborate on it? Unable to understand and so unable to help
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 6
Reputation: catcoffee is an unknown quantity at this point 
Solved Threads: 0
catcoffee catcoffee is offline Offline
Newbie Poster
 
0
  #4
Oct 15th, 2009
Hi darkagn! Thank you for your answers

All problems are in associeted fields

example:
  1. <form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>
  2. //...//
  3. <input type="checkbox" name="pregnancy" value="1"/>
  4. <input type="checkbox" name="onebaby" value="2"/>
  5. <input type="checkbox" name="twobaby" value="3"/>
  6. <input type="text" name="namebaby" />
  7. <input type="text" name="agebaby" />
  8. <input type="text" name="namebaby1" />
  9. <input type="text" name="agebaby1" />
  10. //...//
  11. <input type="submit" name="btn" value="send"/>
  12. </form>
If user choice pregnancy field - fields "namebaby" and "agebaby" not required for user.
If user choice onebaby field - fields "namebaby" and "agebaby" required for user, but fields "namebaby1" and "agebaby1" not required for user.
All fields required for user with 2 babies.

I tried insert script in form and call each field
  1. $value = '1';
  2. echo "<input type='checkbox' name='pregnancy' value='1'/> Pregnancy?";
  3.  
  4. if (isset($_POST['pregnancy'])) {
  5. //
  6. if ($_POST['pregnancy'] == $value) {
  7. $pregnanc = true;
  8. } else {
  9. // No value
  10. $pregnanc = false;
  11. }
  12.  
  13. if ($pregnanc) {
  14. print 'happy';
  15. } else {
  16. echo "<input type='text' name='namebaby' />";
  17. }
it was bad idea
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 101
Reputation: jomanlk is an unknown quantity at this point 
Solved Threads: 18
jomanlk jomanlk is offline Offline
Junior Poster

Form validation

 
1
  #5
Oct 16th, 2009
First off, your HTML form needs to look like this

  1. <form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' enctype="multipart/form-data" action='' method='post'>
  2. //...//
  3. <input type="radio" name="num_babies" value="0"/>
  4. <input type="radio" name="num_babies" value="1"/>
  5. <input type="radio" name="num_babies" value="2"/>
  6. <input type="text" name="namebaby" />
  7. <input type="text" name="agebaby" />
  8. <input type="text" name="namebaby1" />
  9. <input type="text" name="agebaby1" />
  10. //...//
  11. <input type="submit" name="btn" value="send"/>
  12. </form>

Notice the enctype="multipart/form-data" , this tells the browser that a file needs to be encoded with this form.

Next up, you need to change the 'baby' field to a radio button so the user can only select one option. Set the appropriate values (0, 1 and 2) so you know which one is selected.

Your PHP validation should be something like this

  1. <?php
  2. <?php $babyField = isset($_POST['num_babies']) ? $_POST['num_babies'] : 0; //if num babies is set, it takes that value
  3. //otherwise it assigns 0
  4.  
  5. switch ($babyField) { //This will manage our baby types more easily
  6. case 0: //Type is preganancy
  7. //No names are required
  8. break;
  9.  
  10. case 1: //One baby
  11. if ($_POST['namebaby'] == "") {}//Error do something
  12. if ($_POST['agebaby'] == "") {}//Error do something
  13. break;
  14.  
  15. case 2: //two babies
  16. if ($_POST['namebaby'] == "") {}//Error do something
  17. if ($_POST['agebaby'] == "") {}//Error do something
  18. if ($_POST['namebaby1'] == "") {}//Error do something
  19. if ($_POST['agebaby1'] == "") {}//Error do something
  20. break;
  21.  
  22. default: //Just in case someone got smart and put an invalid field. so we
  23. break;
  24. }
  25.  
  26.  
  27. ?>
  28. ?>

Obviously this is a very simple form handling technique. But it should be good enough for your purposes. Close thread if this is the solution or continue with your query
Last edited by jomanlk; Oct 16th, 2009 at 11:26 am. Reason: accidentally submitted before I was done
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 6
Reputation: catcoffee is an unknown quantity at this point 
Solved Threads: 0
catcoffee catcoffee is offline Offline
Newbie Poster
 
0
  #6
Oct 16th, 2009
THANK YOU, jomanlk!
I'm idiot )

For all: thank for your attention and help. Тhe problem have solution thank to jomanlk.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 101
Reputation: jomanlk is an unknown quantity at this point 
Solved Threads: 18
jomanlk jomanlk is offline Offline
Junior Poster
 
0
  #7
Oct 16th, 2009
You're welcome. Please mark the thread as solved if you're done with it. this makes it easier for other people (and I get solve points )
Reply With Quote Quick reply to this message  
Reply

Tags
form, php, upload

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC