Results 1 to 3 of 3

Thread: form submission help please!

  1. #1
    Senior Member ljackson's Avatar
    Join Date
    Feb 2009
    Posts
    570

    Exclamation form submission help please! 31 Dec 2009 @ 13.37

    Hi Guys,

    i have a form which allows users to select an enitial value and then submit it then depending on the result show more textboxes/listboxes and then allow them to submit the data which inturn inserts the data ito a db and displays another page if the textfield has text in it.

    now the problem im having is, is that after the form is submitted it inserts the data in the db fine (i havent setup the form action yet) then if i highlight the url and press enter to reload the page it (tries) to insert the data again??? but it cant because the ID field prevents it because its duplicated.

    so im not sure why its trying to submit the data again?

    and how do i get the form to display another page if all the data is entered correctly? would i use the header function??? thanks

    here is my code (ive removed some of the if statments where the category = something because it will be a lot of code and its not really necessary

    PHP Code:
    $options = array(
        'Selection' => array("All Categories"),
        'Entertainment' => array("DVD","CD","VideoGame","Blu-Ray","Book")
    );

    $sLastGroup = '';
    ?>
    <div style="float:left; padding-left:5px; width:550px; height:65px; background-color:#99CCFF;">
    <?php print "Product Search"?>
    <form id="form1" name="form1" method="post" action="">

    <?php #initial item count done here
            
    $AW_catid "";
            
    $AW_cat "";
            
    $keywords "";
            
    $oRefineBy = new stdClass();
            
    $oRefineBy -> iId 4;
            
    $oRefineBy -> sName 'Category';
            
    $oRefineByDefinition = new stdClass();
            
    $oRefineByDefinition -> sId $AW_catid;
            
    $oRefineByDefinition -> sName $AW_cat;
            
    $oRefineBy -> oRefineByDefinition $oRefineByDefinition;
                
            
    $aParams = array("sQuery" => "$keywords""bIncludeTree" => true"oActiveRefineByGroup" => $oRefineBy);    
            
    $oResponse$oClient->call('getProductList'$aParams);
            
    $iTotalCount $oResponse->iTotalCount// get the total count before the next call

        #CATEGORY LISTBOX VALUES ADDED HERE
    ?>
        <label>
    <select name="category" id="category">
          <?php
    $sOptions 
    '';
    foreach (
    $options AS $group => $items)
    {
        
    $sInnerOptions '';
        if (
    is_array($items))
        {
            foreach (
    $items AS $item)
            {
                
    $sSelected '';
                
    $cat = &$_REQUEST['category']; // Use post or get if you can, they are better for control
                
    if (strcmp($cat$item) === 0)
                {
                    
    $sSelected ' selected="selected"';
                }
                
    $sInnerOptions .= sprintf('<option value="%s"%s>%s</option>' PHP_EOLurlencode($item), $sSelected$item);
            
            }  
        }
        if (
    $sLastGroup != $group)
        {
            
    $sLastGroup $group;
            
    $sOptions .= sprintf('<optgroup label="%s">%s</optgroup>' PHP_EOL$group$sInnerOptions);
        }
    }

    print 
    $sOptions;
    ?>
    </select>
    </label>

    <?php 
        
        
    if(isset($_POST['submit']))
        {
            if(isset(
    $_POST['category']))
            {
                
    $category urldecode($_POST['category']);
            }
                else 
                {
                    
    $category "All Categories";
                }
        }
        
    #IF THE USER DOESNT DEFINE A CATEGORY
        
    if($category == "All Categories")
        {
            
    $AW_catid "";
            
    $AW_cat "";
            
    $keywords "";
            
    $oRefineBy = new stdClass();
            
    $oRefineBy -> iId 4;
            
    $oRefineBy -> sName 'Category';
            
    $oRefineByDefinition = new stdClass();
            
    $oRefineByDefinition -> sId $AW_catid;
            
    $oRefineByDefinition -> sName $AW_cat;
            
    $oRefineBy -> oRefineByDefinition $oRefineByDefinition;
                
            
    $aParams = array("sQuery" => "$keywords""bIncludeTree" => true"oActiveRefineByGroup" => $oRefineBy);    
            
    $oResponse$oClient->call('getProductList'$aParams);
            
    $iTotalCount $oResponse->iTotalCount// get the total count before the next call        
            
            
    if(isset($_POST['category'])){?>
            <label>
            <input name="search_input" type="text" id="search_input" value="<?php echo $_POST['search_input']; ?>"/>
            </label>
            <?php
            
    }
        } 
        
        
    #IF THE USER SET DVD AS THE CATEGORY
        
    if($category == "DVD" || $category == "Blu-Ray")
        {
        if(isset(
    $_POST['category']) && isset($_POST['search_input']) && $_POST['search_input'] == ""){
            
    $AW_catid "235";
            
    $AW_cat "DVDs";
            
    $keywords "";
            
    $oRefineBy = new stdClass();
            
    $oRefineBy -> iId 4;
            
    $oRefineBy -> sName 'Category';
            
    $oRefineByDefinition = new stdClass();
            
    $oRefineByDefinition -> sId $AW_catid;
            
    $oRefineByDefinition -> sName $AW_cat;
            
    $oRefineBy -> oRefineByDefinition $oRefineByDefinition;
                
            
    $aParams = array("sQuery" => "$keywords""bIncludeTree" => true"oActiveRefineByGroup" => $oRefineBy);    
            
    $oResponse$oClient->call('getProductList'$aParams);
            
    $iTotalCount $oResponse->iTotalCount// get the total count before the next call        
            
    }
            
    $dvd_searchby = array("Title","Actor","Director");
            if(
    $category == "Blu-Ray"){
            
    $extra "bluray";
            }
            else
            {
            
    $extra "";
            }
            
            
    ?>
                        <label>
                        <input type="text" name="search_input" id="search_input" value="<?php echo $_POST['search_input']; ?>" size="26"/>
                        </label>
                        
                        <label>
                        <select name="dvd_search_list" id="dvd_search_list">
                        <?php
                        
    foreach ($dvd_searchby as $search_list)
                        {
    ?>
                        <option value="<?php echo $search_list?>"<?php 
                        
    if($search_list==@$_POST['dvd_search_list'])
                        {
    ?>
                            selected="selected"<?php 
                        
    }
                        
    ?>><?php echo $search_list?></option><?php
                        
    }
                        
    ?>
                        </select>
                        </label>
                        <?php
                                
            $keywords 
    $_POST['search_input']." ".$extra;
            
    $oRefineBy = new stdClass();
            
    $oRefineBy -> iId 4;
            
    $oRefineBy -> sName 'Category';
            
    $oRefineByDefinition = new stdClass();
            
    $oRefineByDefinition -> sId '235';
            
    $oRefineByDefinition -> sName 'DVDs';
            
    $oRefineBy -> oRefineByDefinition $oRefineByDefinition;
            
    $returnedcolumns = array(sName,sAwImageUrl,sMerchantImageUrl,sMerchantThumbUrl,sDescription,sBrand,iMerchantId,iCategoryId);
            
    $aParams = array("sQuery" => "$keywords""bIncludeTree" => true"sColumnToReturn" => $returnedcolumns"oActiveRefineByGroup" => $oRefineBy"iLimit" => 100);    
            
    $oResponse$oClient->call('getProductList'$aParams);
            
    $iTotalCount $oResponse->iTotalCount// get the total count before the next call
            
        
    }?>
              
        <label>
        <input type="submit" name="submit" id="submit" value="Submit" />
        </label>    
        </form>
        
        <?php
        
    #PUT RESULTS INTO A UNIQUE ARRAY SO NOT DUPLICATED
        
    if(isset($_POST['submit']) && isset($_POST['search_input']) && $_POST['search_input'] <> "")
        {
            
    $products_array = array();
            
    $x 0;
            foreach(
    $oResponse->oProduct as $oProduct)
            {
                
    $product_name strtolower($oProduct->sName);
                
    #$short_product_name = preg_replace("/\s*\(.*\)/", "", $product_name);
                
    $products_array[$x]["ID"] = $oProduct->iId;
                
    $products_array[$x]["Name"] = $product_name;
                
    $x++;
            }

            
    $unique_products = array();
              
    $exclude = array("");
              for (
    $i 0$i<=count($products_array)-1$i++)
            {
                if (!
    in_array(trim($products_array[$i]["Name"]) ,$exclude)) 
                {
                    
    $unique_products[] = $products_array[$i];
                    
    $exclude[] = trim($products_array[$i]["Name"]);
                }
              }

                
    #TRY AND DISPLAY THE IMAGES/LINKS HERE
                
    $x 0;
                foreach(
    $unique_products as $product)
                {
                
    // the columns to return in both our calls
                
    $returnedcolumns = array(sDescription,sSpecification,sPromotion,sBrand,sModel,iMerchantId,fPrice,fRrpPrice,iId,sAwImageUrl,sMerchantImageUrl,sMerchantThumbUrl,iCategoryId);
                
    // the parameters for the single product call
                
    $getsingleproduct = array('iProductId' => $product['ID'], "sColumnToReturn" => $returnedcolumns"bAdult" => true"iLimit"=>1);
                
    $oGetProduct$oClient->call('getProduct'$getsingleproduct); // the single product call
                
                
    $id $product['ID'];
                
    $name strtolower($oGetProduct->oProduct->sName);
                
    $name preg_replace("/\s*\(.*\)/"""$name);
                
    $image $oGetProduct->oProduct->sMerchantImageUrl;
                
    $link $oGetProduct->oProduct->sAwDeepLink;
                
    $price $oGetProduct->oProduct->fPrice;
                
    $description $oGetProduct->oProduct->sDescription;
                
    $store $oGetProduct->oProduct->iMerchantId;
                
    $specs $oGetProduct->oProduct->sSpecification;
                
    $brand $oGetProduct->oProduct->sBrand;
                
    $model $oGetProduct->oProduct->sModel;
                print 
    "<p>";
                print 
    $id;
                print 
    "<br />";
                print 
    "<img src='$image' title='$id'/>";
                print 
    "</p>";
                
    $insertdata "INSERT INTO
                product_search_results (ProdID, ProdName, ProdIMG, ProdLink, ProdPrice, ProdDesc, StoreID, ProdSpecs, ProdBrand, ProdModel) VALUES ('
    $id', '$name', '$image', '$link', '$price', '$description', '$store', '$specs', '$brand', '$model')";
                
    $query mysql_query($insertdata)or die(mysql_error());
                }
                
        }
        print 
    "Total Products Found: ".$iTotalCount;
        
    ?>
    any help would be appreciated
    cheers
    Luke
      Reply With Quote

  2. #2
    Super Moderator CloudedVision's Avatar
    Join Date
    Jan 2009
    Posts
    831
    Blog Entries
    4

    Default 3 Jan 2010 @ 21.11

    You can redirect the user by using:

    PHP Code:
    session_write_close(); //Only use this line if your using sessions, otherwise exclude it.
    header('Location: http://www.mysite.com/awesome/cool.php');
    exit; 
    However, you cannot have outputted any text previous to using this command, otherwise it'll return an error.
      Reply With Quote

  3. #3
    Senior Member ljackson's Avatar
    Join Date
    Feb 2009
    Posts
    570

    Default 10 Jan 2010 @ 22.14

    hiya mate, sorry forgot about this thread... solved the problem in the end was something to do with the order of the code? cant remember the exact fix now but it seems to be working

    cheers
    Luke
      Reply With Quote

Similar Threads

  1. Page Refresh upon form submission?
    By Vacant Brains in forum PHP, ASP & Java
    Replies: 3
    Last Post: 3 Apr 2011, @ 19.13
  2. IE Misbehaving on Form Submission
    By Frinkky in forum PHP, ASP & Java
    Replies: 10
    Last Post: 5 Aug 2010, @ 09.57
  3. form submission help please.
    By ljackson in forum PHP, ASP & Java
    Replies: 19
    Last Post: 21 Jul 2010, @ 22.17
  4. Replies: 9
    Last Post: 18 Sep 2009, @ 15.53

Tags for this Thread

Posting Permissions

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