Page 1 of 1

php functions and forms.

Posted: Mon Nov 17, 2008 11:30 pm
by Weasel5-12
Hi, Im developing an online auction site for an assignment as part of my final year project for uni.

And i was wondering if it were possible to instead of, when using forms, sending the data taken from the user ie. a checkbox, and send it to a function vs. sending to a new page(script)??

Here's what i've got so far.

Code: Select all

 [color=#0000FF]function[/color] addAdmin($info[[color=#FF0000]'UserID'[/color]]){
    $userID = $info[[color=#FF0000]'UserID'[/color]];
        
    $query = [color=#FF0000]"UPDATE users SET Type = 'Admin' WHERE UserID =2 LIMIT 1"[/color];
    $result = [color=#0000FF]mysql_query[/color]($query)[color=#0000FF] or die[/color] ([color=#FF0000]"Couldn't execute query"[/color]);
}
...
...
...
$query = [color=#FF0000]"SELECT * FROM users ORDER BY Username ASC"[/color];
$result = [color=#0000FF]mysql_query[/color]($query);
            
[color=#0000FF]echo[/color] [color=#FF0000]"<form action='"[/color].addAdmin().[color=#FF0000]"' method='post'>"[/color];
            
[color=#008000]while[/color]($info = [color=#0000FF]mysql_fetch_array[/color]( $result )){
    [color=#FFBF00]// Output Check List[/color]
    [color=#008000]if[/color]($info['Type'] != [color=#FF0000]"Admin"[/color]){
        [color=#0000FF]echo[/color] [color=#FF0000]"<input type='checkbox' name='Username[]' value='"[/color].$info['Username'].[color=#FF0000]"'> "[/color].$info['Username'].[color=#FF0000]"<br>"[/color]; 
        [color=#0000FF]echo[/color] [color=#FF0000]"<input type='hidden' name='hidden' value='"[/color].$info['UserID'].[color=#FF0000]"'"[/color];
    }       
}

Re: php functions and forms.

Posted: Mon Nov 17, 2008 11:42 pm
by requinix
It's called "AJAX".

Google for it, you'll find a lot.

Re: php functions and forms.

Posted: Tue Nov 18, 2008 2:50 pm
by Syntac
Oh, and did you know the syntax highlighting is automatic? Just use [syntax=php].[/syntax]

Re: php functions and forms.

Posted: Tue Nov 18, 2008 3:00 pm
by aceconcepts
You don't actually have to use action in a form. Simply post the form to itself and validate from the top of the script.

i.e.

Code: Select all

 
//validate form post
if(isset($_POST['submit']))
{
  //do something
}
 
echo'<form name="form1" method="post">';
  echo'<label>Name</label><input type="text" name="strName" /><br />';
  echo'<input type="submit" name="submit" value="Submit" />';
echo'</form>';
 

Re: php functions and forms.

Posted: Tue Nov 18, 2008 4:57 pm
by requinix
aceconcepts wrote:You don't actually have to use action in a form. Simply post the form to itself and validate from the top of the script.
Au contraire, the action is the one required attribute of a form. Method, name, id... those are all optional.

Re: php functions and forms.

Posted: Tue Nov 18, 2008 5:45 pm
by aceconcepts
If you omit action from the form's paramaters it defaults to itself - give it a go :wink:

Re: php functions and forms.

Posted: Tue Nov 18, 2008 5:55 pm
by requinix
aceconcepts wrote:If you omit action from the form's paramaters it defaults to itself - give it a go :wink:

Code: Select all

<!ELEMENT FORM - - (%block;|SCRIPT)+ -(FORM) -- interactive form -->
<!ATTLIST FORM
  %attrs;                              -- %coreattrs, %i18n, %events --
  action      %URI;          #REQUIRED -- server-side form handler --
  method      (GET|POST)     GET       -- HTTP method used to submit the form--
  enctype     %ContentType;  "application/x-www-form-urlencoded"
  accept      %ContentTypes; #IMPLIED  -- list of MIME types for file upload --
  name        CDATA          #IMPLIED  -- name of form for scripting --
  onsubmit    %Script;       #IMPLIED  -- the form was submitted --
  onreset     %Script;       #IMPLIED  -- the form was reset --
  accept-charset %Charsets;  #IMPLIED  -- list of supported charsets --
  >
http://www.w3.org/TR/1999/REC-html401-1 ... tml#h-17.3
It defaults because that's what your browser's developers decided to do.

My car can do 90mph but that doesn't mean I should.

Re: php functions and forms.

Posted: Tue Nov 18, 2008 6:00 pm
by Syntac
Include it, because you should.

Re: php functions and forms.

Posted: Tue Nov 18, 2008 6:01 pm
by aceconcepts
Honestly, try it. It works just fine.

Although it's good to know that technically it's a required attribute :wink:

Re: php functions and forms.

Posted: Wed Dec 03, 2008 1:25 am
by Weasel5-12
I found a seperate script to use instead.

Code: Select all

..
$locaction = $_SERVER['PHP_SELF'];
echo "<form method='post' action='".$locaction."'>";
..