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

Recommended Answers

All 6 Replies

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?

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 :(

Hi darkagn! Thank you for your answers :)

All problems are in associeted fields

example:

<form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>
//...//
<input type="checkbox" name="pregnancy" value="1"/>
<input type="checkbox" name="onebaby" value="2"/>
<input type="checkbox" name="twobaby" value="3"/>
<input type="text" name="namebaby" />
<input type="text" name="agebaby" />
<input type="text" name="namebaby1" />
<input type="text" name="agebaby1" />
//...//
<input type="submit" name="btn" value="send"/>
</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

$value = '1';
echo "<input type='checkbox' name='pregnancy' value='1'/> Pregnancy?";

if (isset($_POST['pregnancy'])) {
    // 
    if ($_POST['pregnancy'] == $value) {
        $pregnanc = true;
} else {
    // No value
    $pregnanc = false;
}

if ($pregnanc) {
    print 'happy';
} else {
    echo "<input type='text' name='namebaby' />";
}

it was bad idea

First off, your HTML form needs to look like this

<form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' enctype="multipart/form-data" action='' method='post'>
//...//
<input type="radio" name="num_babies" value="0"/>
<input type="radio" name="num_babies" value="1"/>
<input type="radio" name="num_babies" value="2"/>
<input type="text" name="namebaby" />
<input type="text" name="agebaby" />
<input type="text" name="namebaby1" />
<input type="text" name="agebaby1" />
//...//
<input type="submit" name="btn" value="send"/>
</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

<?php
<?php	$babyField	= isset($_POST['num_babies']) ? $_POST['num_babies'] : 0; //if num babies is set, it takes that value
																			//otherwise it assigns 0

	switch ($babyField) { //This will manage our baby types more easily
		case 0: //Type is preganancy
				//No names are required
			break;

		case 1:	//One baby
			if ($_POST['namebaby'] == "") {}//Error do something
			if ($_POST['agebaby'] == "") {}//Error do something
			break;

		case 2:	//two babies
			if ($_POST['namebaby'] == "") {}//Error do something
			if ($_POST['agebaby'] == "") {}//Error do something
			if ($_POST['namebaby1'] == "") {}//Error do something
			if ($_POST['agebaby1'] == "") {}//Error do something
			break;

		default:	//Just in case someone got smart and put an invalid field. so we
			break;
	}


?>
?>

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 :)

THANK YOU, jomanlk!
I'm idiot :))

For all: thank for your attention and help. Тhe problem have solution thank to jomanlk.

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 :) )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.