Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use functions on forms 2

Status
Not open for further replies.

hos2

Programmer
Joined
May 6, 2002
Messages
418
Location
NL
I have a form which uses the sqlaction.php3 when submitted. I was now wondering if I could not incorporate it as a function and that after finishing the function the same page is reloaded where it was submitted on ??

Is it possible/recommend to use functions instead of files to execute ?? or has it disadvantages ??
 
Sure, in fact I believe that's a very common practice. What you need to do is two things:

1) Add the code from sqlaction.php3 to yur form page and wrap it in an if statement.
2) Add a querystring variable to the form action attribute.

Code:
<?php
    if ($_GET['action'] == 'save')
    {
// place your code here to handle the form
    }
    else
    {
//otherwise display form.
?>
<html>
    <head></head>
    <body>
        <form method=&quot;post&quot; action=&quot;?action=save&quot;>
<!- your form stuff here
        </form>
    </body>
</html>

That should do it :)

Take Care,
Mike
 
hmmm this construction doesn't quite work ?? is something wrong in the action=&quot;?action=save&quot;>

code ???? it looks strange
 
This is how I do it. I believe the 'action' part posted is typoed a bit too.
Code:
<?php
switch($_POST['action']) {
    case &quot;save&quot;:
    // place your code here to handle the form
    break;

    case &quot;delete&quot;:
    // you could delete a record or something here
    break;

    default:
    //catchall default
?>
<html>
    <head></head>
    <body>
        <form method=&quot;post&quot; action=&quot;save&quot;>
<!- your form stuff here
        </form>
    </body>
</html>
<?php
}
?>


----
JBR
 
ah this thread has also a nice way
thread434-733765 try them tonight :)
 
No, my code above is correct:

action=&quot;?action=save&quot; means that the form will post to itself, since there is no filename in from of the querystring. After the '?' the querystring is defined with a variable called 'action' being set to 'save'.

So from there you can evaluate that you are saving after you hit the submit button by checking the querystring variable 'action' using $_GET['action'] to see if it is set to save.

If you don't have the querystring variable at all in the url, or it is not set to save, then it won't go into the save routing and will default to displaying the form.

Don't knock it before you try it :).

Take Care,
Mike
 
yep I have tried it but now I realize that I normally pass the keyfield with the action. that one is now unavailable.

the function that has to be performed after submit looks like this

Code:
// the key is passed through the url with the name of the keyfield
list($tablekey, $keyvalue) = each($HTTP_GET_VARS);


//array for attaching the keynames to the corresponding tablenames
$tablearray=array();
$tablearray[arid]='artisttable';
$tablearray[clid]='clienttable';
$tablearray[cltid]='clienttypetable';
$tablearray[orid]='ordertable';
$tablearray[orlid]='orderlinestable';


//loop through the posted variables
//formfieldnames must be corresponding to tablefieldnames
//in case of array than arrayname must be corresponding fieldname

while ( list($field, $value) = each ($HTTP_POST_VARS)) {
	if ($field <> &quot;Submit&quot; ){
		if (is_array($value)){

				$arrayLength = count($value);
				$temparray=array_values($value);	
				for ($i = 0; $i < $arrayLength; $i++){ 
						$temp=$temp .$temparray[$i] .&quot;;&quot;;
						}
					$fieldlines=$fieldlines .$field .&quot;='&quot; .$temp .&quot;',&quot;;
			}else{
			$fieldlines=$fieldlines .$field .&quot;='&quot; .$value .&quot;',&quot;;
			}
		
				
	}
    }


$fieldlines=substr($fieldlines,0,strlen($fieldlines)-1); // get rid of the last ,


if (!empty($keyvalue)) //if no key in the url then consider it an insert statement
	{
	$query=&quot;UPDATE &quot; . $tablearray[$tablekey] .&quot; SET &quot; . $fieldlines . &quot; WHERE &quot; . $tablekey .&quot;='$keyvalue'&quot;;
	}
	else
	{
	$query=&quot;INSERT INTO &quot; . $tablearray[$tablekey] . &quot; SET &quot;. $fieldlines;
	}

$rs=mysql_query($query,$conn);

I want to put above part in an overall function in an include file and call upon that function after submit.

since 'save' is now in the get I have to look how to rewrite things
 
You can pass the keyfield like this:
Code:
<form method=&quot;post&quot; action=&quot;?action=save&quot;>
    <input type=&quot;hidden&quot; name=&quot;key&quot; value=&quot;<?= $key ?>&quot;>
</form>

and then retrieve it on the next page using: $_POST['key']

Take Care,
Mike
 
yep I also thought of the hidden post command but I only separeted them into GET and POST to distinct the keyfield from the normal fields. now I have to somehow determine which field is the keyfield when looping through the posted variables. and get the name and value out of the string fieldlines.
hmmm perhaps use

<input type=&quot;hidden&quot; name=&quot;tablekey&quot; value=&quot;<?= $keyname ?>&quot;>
<input type=&quot;hidden&quot; name=&quot;keyvalue&quot; value=&quot;<?= $keyvalue ?>&quot;>

and then filter them out in the fieldlines function. retry tonight ;)
 
hmmmmm something goes wrong with the or field

Code:
while ( list($field, $value) = each ($HTTP_POST_VARS)) {
	if ($field <> &quot;Submit&quot; ){
		if (is_array($value)){

				$arrayLength = count($value);
				$temparray=array_values($value);	
				for ($i = 0; $i < $arrayLength; $i++){ 
						$temp=$temp .$temparray[$i] .&quot;;&quot;;
						}
					$fieldlines=$fieldlines .$field .&quot;='&quot; .$temp .&quot;',&quot;;
			}else{
			if (($field != &quot;keyvalue&quot;) OR ($field != &quot;tablekey&quot; ))
				{	
				$fieldlines=$fieldlines .$field .&quot;='&quot; .$value .&quot;',&quot;;
				}
			if($field==&quot;tablekey&quot;) {$tablekey=$value;}
			if($field==&quot;keyfield&quot;) {$keyfield=$value;}
			}
		
				
	}
    }

I want to leave out both tablekey and keyfield from the fieldlines :(

does anybody have an idea ???
 
aarrggh if I make it an AND it works fine. my php is going nuts or I am
 
only disadvantage of you're method is that after a reload of the page the page is submitted again since action=save is in the url :(
 
It's you.

Think what the OR (or more correctly in this context, &quot;||&quot;) would do. If either disjunct (the things being operated on by the &quot;||&quot;) is true, then the script outputs. But the field can be named only one name or the other, so at least one of the disjuncts will always evaluate to true.

Now, consider &quot;&&&quot;. In order for the entire clause to be true, both conjuncts (the things being operated on by the &quot;&&&quot;) would have to be not either of the two names. This is what you want.

About the different operators. In this context, you should use &quot;||&quot;, not &quot;OR&quot;. Although the two operators perform the same function, &quot;||&quot; has a higher operator precedence than &quot;||&quot;, so in some cases you can get some weird behavior using the wrong one.



Want the best answers? Ask the best questions: TANSTAAFL!!
 
hmmm strange I also tried the || but it still didn't work :( but I try again ;)

ps how can I empty HTTP_POST_VARS and test when it's not empty ??
since after a reload everything is submitted again so a new record is inserted. when it's empty it would just continue
 
That's because &quot;OR&quot; and &quot;||&quot; perform the same logical function. If either disjunct is true, then the clause evaluates to true. An array element can only be named one thing, so regardless of which logical or operator you use, one of the disjuncts will always evaluate to true, so the entire clause will always evaluate to true.

But use &quot;||&quot; in this context. &quot;OR&quot; is generally used in an &quot;operational&quot; context:

$dbh = mysql_connect(....) or die(&quot;Could not connect...&quot;);


With logical operator optimization, the second disjunct will only be invoked if the first disjunct evaluates to falue.


If no data has been submitted via POST-method input, then $HTTP_POST_VARS (or more correctly, $_POST) will have no elements. count($_POST) should then be zero.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
yep but when I submit the page with for instance a new record and after that I do a reload of the same page the record is submitted twice :( so HTTP_POST_VARS is not empty then
 
That would be because your browser is resubmitting the data for you automatically.

You could use session values to track the last submission to the last script. If the identical information is submitted to the script again, ignore the second submission.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
and how do I do that ??
 
Whenever data is submitted, before processing check the session variables to see what was submitted where the last time. If the data currently submitted is identical to the previous data, and if that data was submitted to a script with the same name as the current script, ignore the input.

Otherwise, process the input then store in one session variable the name of the script to which the data was submitted. Store in another session variable the data submitted to that script. Or better, store an MD5 checksum of some standard representation of that data.

Do this every time data is submitted.


Want the best answers? Ask the best questions: TANSTAAFL!!
 
is it not possible to say
if($dummy==$HTTP_POST_VARS)
{ignore etc}
else
{submit code
$dummy=$HTTP_POST_VARS
}

??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top