SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    two buttons in a form

    Hi,

    I have two buttons, one is for submitting, one is for doing something else.

    So far, the one supposed to do something else, do the submit too, that's the problem.

    HTML Code:
    <input type="submit" name="submit" id="submit" value="Make change" />
    
    <input type="submit" name="cancel" id="cancel" value="Cancel" />
    Code PHP:
    If($_POST['submit']) { change the info }
     
    If($_POST['cancel'] { go back to previous page }

    If you have an answer for me, I'd also appreciate to hear how to handle 3 buttons.

  2. #2
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That should work. Post your actual code instead of retyping what you think your code is. It's probably a stupid mistake.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  3. #3
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The full code for processing form, this is for changing user's information. After done, user is redirected back to user section. If cancel, user will be also back to user section.

    The form validates the input first then processes the change to database.

    Code PHP:
    $errors = array();
    $url = URL;
    $username = $_COOKIE['active']['username'];
     
    if(isset($_POST['submit']))
    {
    	$fields = array('email','name', 'land_line', 'address', 'city', 'region');
     
    	foreach($fields as $field)
    	{
    		${$field} = mysql_real_escape_string(trim($_POST[$field]));
    	}
     
    	if(empty($email)) {$errors[] = ' need email';}
     
    	if(empty($name)) {$errors[] = 'need name';}
     
    	if(empty($land_line)) {$errors[] = 'need land_line number';}
     
    	if(empty($address)) {$errors[] = 'need address';}
     
    	if(empty($city)) {$errors[] = 'need city';}
     
    	if(!empty($email) && !empty($name) && !empty($land_line) && !empty($address) && !empty($city) )
    	{
    		$query2 = "UPDATE user SET 
    					email = '{$email}'
    					, name = '{$name}'
    					, land_line = {$land_line}
    					, address = '{$address}'
    					, city = '{$city}'
    				WHERE username = '{$username}' ";
    	}
    	$result2 = mysql_query($query2) or die(mysql_error());
     
    	if($result2)
    	{
    		header('location:'.$url.'account.php');
    		exit();
    	}
    }
    if(isset($_POST['cancel'])) 
    {
    	header('location:'.$url.'account.php');
    	exit();
    }

    I've just edited the code slightly and I have this error

    Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ')' in /home/badede/public_html/php/change_info.script.php on line 26

    When I don't have error. The two buttons function the same thing, which is undesirable.

  4. #4
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure, your code works pretty much verbatim when I tested it.

    http://bradhanson.net/tmp/post.php
    http://bradhanson.net/tmp/post.phps
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I also did not find any problem with your posted code.

    But regarding the buttons, if you don't need to process anything with another button then simply you can use javascript to redirect the browser to the user section directly.
    HTML Code:
    <input type="submit" name="submit" id="submit" value="Make change" />
    
    <input type="button" name="btnCancel" value="Cancel" onClick="document.location='usersection.php';" />
    Otherwise if you still want to be safe and redirect from PHP, then the process you have done is OK:
    HTML:
    HTML Code:
    <input type="submit" name="btnSubmit" value="Make change" />
    <input type="submit" name="btnSubmit" value="Cancel" />
    Note the same name of both the buttons.
    PHP:
    PHP Code:
    if($_POST['btnSubmit'] == 'Cancel'){
        
    header('Location:usersection.php');
        exit();
    }
    else if(
    $_POST['btnSubmit'] == 'Make change'){
        
    //do some process here.

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you guys.

    Acutally, itsn't a problem.

    Because there are some javascript validation in html form, thats why when I click cancel it prompts me to finish some empty textfields, making me think it is a submitting. How silly!
    Last edited by runrunforest; Dec 8, 2008 at 02:14.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •