Jump to content

[SOLVED] Running through textfields without specifying name of textfield


peddel

Recommended Posts

Im looking for the following :

 

When a person hits the submit button of a form,

the form gets all the entered values from the textfields and puts them in session variables.

Now because a form in advance of that one defines how many textfields there will be, depending on the input on the previous form,

i cant just use functions like $_POST['textfieldname'] becuz i dont know in front which fields will be displayed.

 

Anyone can give me a clue how to solve this problem?

Link to comment
Share on other sites

What form fields are available?  Are they all related or are they different field names completely?  If you don't know what filed names are being passed what are you going to do with the information?  Can you explain exactly what you are trying to do.

Link to comment
Share on other sites

well lets asume the following

 

FORM 1

-------

Person inserts name of a pressure valve and the value with it

TEXTFIELD1 : Please enter the valve name : <input type='text' name='valvename' />

TEXTFIELD2 : Please enter the value : <input type='text' name='value' />

 

Then the person gets redirected to a page where a checkup happens when he submitted the 1ste form

 

FORM2

-------

Value has to be between upper and lower value.

With MySQL i retrieve these values from a database and compare em with $_POST['value']

Till here it still works.

 

NOW when the value is not between the border values i do the following :

Please enter a new value for the valve = <input type='text' name='valueNew' />

 

Immagine the above situation counts for like 10 fields. This means that on form2 there will be sometimes

0 requests for new value, or there will be 10 values asked.

 

I submit the new form with the new values and go to FORM3

 

FORM3

------

Now has to know what values where changed so he can put them away in a session variable.

BUT how can he know what fields where on form2 and which were not :D

SO i cant use the $_POST['valueNew'] CUZ i dont know if that field was shown our not

 

 

HOPE this helps :P

Link to comment
Share on other sites

I would just collect all your pressurre valve inputs in one array and the values in another from form1.

 

TEXTFIELD1 : Please enter the valve name : <input type='text' name='valvename[]' />

TEXTFIELD2 : Please enter the value : <input type='text' name='value[]' />

 

Then you can use the keys from the array to know which one need to be changed. 

Link to comment
Share on other sites

Use a foreach loop:

<?php
foreach ($_POST as $fld => $val) {
//
// do your work
//
}?>

 

Ken

 

can u explain once i run trough all those $_POST variables

how i can know the name of the field that he is in for that moment?

becus i would do something like this

 

foreach ($_POST as $fld => $val) {
  //line to get the "name of the field"
  $var = $_SESSION['name of the field']
}?>

 

ALREADY big thx for the help

Link to comment
Share on other sites

I would just collect all your pressurre valve inputs in one array and the values in another from form1.

 

TEXTFIELD1 : Please enter the valve name : <input type='text' name='valvename[]' />

TEXTFIELD2 : Please enter the value : <input type='text' name='value[]' />

 

Then you can use the keys from the array to know which one need to be changed. 

 

I will try this one if i cant get kens idea working in my script

Link to comment
Share on other sites

I used this code in the php page i go to when form is submitted.

The result was just a blank page !

 

CODE :

<?php
    foreach($_POST as $fld => $val){
    	print($fld);
	print($val);
	print("<br />");
    }
?>

 

What am i missing here?

Link to comment
Share on other sites

You could do something like this:

 

<?php
$valve_name = $_POST['valvename'];
$valve_value = $_POST['value'];

for($i=0; $i<count($valve_name); $i++)
{
//this is where you will check your DB with $valve_name[$i] and $valve_value[$i].

}

?>

 

Maybe I am misunderstanding what yuou need to do or how your form1 is setup.

Link to comment
Share on other sites

You could do something like this:

 

<?php
$valve_name = $_POST['valvename'];
$valve_value = $_POST['value'];

for($i=0; $i<count($valve_name); $i++)
{
//this is where you will check your DB with $valve_name[$i] and $valve_value[$i].

}


?>

 

Maybe I am misunderstanding what yuou need to do or how your form1 is setup.

 

I think u dont get what i mean with my setup,

and i dont really know how to explain it better.

I think KEN gots a clue on what im doing, i just want him to check where i go wrong now.

No offense tough ninedoors! But i think ur thing isnt doing what i want :P

Link to comment
Share on other sites

At the top of your test script, put

<?php
echo '<pre>' . print_r($_POST,true) . '</pre>';
?>

 

This will show you what is coming from the form via the $_POST array. This assumes that you have 'method="post"' in your form tag. If you have method="get" or no method change all references of $_POST to $_GET

 

Ken

Link to comment
Share on other sites

now im totally confused  ???

 

i got the form made like this

 

<form name='formulier1' action=''>

action not filled so it checks the same page again

</form>

 

then inside the form i got a part between

if(!isset($_POST['submit'])){  ... enter code here ... }

and a part between

if(isset($_POST['submit'])){  ... enter code here ... }

 

In the second part i enter that code u gave me, for showing the submitted $POST array

 

It show up as a empty array tough i see the fields visual :S

Link to comment
Share on other sites

You don't have a method in the form tag, so the method defaults to GET and submitting will populate the $_GET array not the $_POST array. Either change all the references of $_POST to $_GET in your script or add a method to the form tag:

<form name="formulier1" action="" method="post">

 

Ken

Link to comment
Share on other sites

You don't have a method in the form tag, so the method defaults to GET and submitting will populate the $_GET array not the $_POST array. Either change all the references of $_POST to $_GET in your script or add a method to the form tag:

<form name="formulier1" action="" method="post">

 

Ken

 

thx man :) found out indeed that was the problem >_<

Still its pretty dumb, making those minor stupid mistakes and searching on it for hours :)

 

THX for helping my problem is solved !

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.