SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    May 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    header(location:url) anchor being added

    Hi

    I have a form which submits to itself: signup#user_result.

    If there's an error with the form, the viewport will show div#user_result at the top.

    If there's no error I redirect to the next signup page:

    PHP Code:
    header('location:signup2'); 
    Weird thing is, and I've no idea why or how to sort it out, the hash anchor is included in the redirect. So the header('location...) actually takes you to

    PHP Code:
     header('location:signup2#user_result'); 
    I'd really like to stop it including the #user_result bit - does anyone know how to do this?

    Thanks very much.

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Welcome to the forums
    can you show us the code you are using?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Member
    Join Date
    May 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers for taking a look

    The php on the page is:

    PHP Code:
    require("lib/form.class.php");

    $frm=new form('captcha');
    $frm->submitImage='graphics/signup.gif';
    $input = new captcha('capo');
    $input->required=true;
    $frm->add_input($input);

    $frm->activate();

    if(
    $frm->success){
    session_write_close();
    header("signup2");
    exit();

    Which uses the form class (it's become a bit bloated over time, but essentially it just outputs and process all my forms...)

    PHP Code:
    /* get dependent files */

    require('lib/inputs.class.php');

    if(!
    $config['includes']['dao']){
    require(
    'lib/dao.php');
    }

    if(!
    $config['includes']['functions']){
    require(
    'lib/functions.php');
    }

    $config['includes']['form']=true;

    class 
    form{

    var 
    $action//url
    var $method "post"//if you change this, change all the values all the way through - its not automatic
    var $name//form name
    var $inputs = array(); //array of inputs

    //if you are adding / editing a table
    var $table;
    var 
    $primaryColumn;

    //if a record is being edited
    var $editID;
    var 
    $populate=true//if there's an edit id populate data

    var $success false//whether form submission went ok

    var $process_function//a function that can be passed into process the form

    var $result//user result messages
    var $onAddResult;
    var 
    $onUpdateResult;
    var 
    $onSendResult;

    var 
    $submitText 'save';
    var 
    $submitImage//submit button image

    //email notification
    var $notify//array of people to email
    var $messageFormat;
    var 
    $messageSubject='from website';
    var 
    $messageFrom;

    var 
    $noSubmit//display submit button
    var $outputOnSuccess=true//output the form on success
    var $clear//clear the post variables on completion

    //constructor
    function form($name){

    global 
    $config;

    $this->name=$name;
    $this->action str_replace('//','/',$_SERVER['REQUEST_URI']); //url

        
    if (get_magic_quotes_gpc()) {
        
    $_POST stripslashes_deep($_POST);
        
    $_GET stripslashes_deep($_GET);
        }

    $this->messageFrom=$config['admin'];

    //required files for form.class

    }

    //add an input to the input array
    function add_input($input){
    $this->inputs[]=$input;

        if(
    is_a($input,'file_input') || is_a($input,'image_input')){
        
    $input->activate();
        }

    }


    //output the form and inputs
    function output_form() { 

        if(
    $this->success &! $this->outputOnSuccess){
        
            if(
    $this->result){
            echo 
    "<div id=\"user_result\">{$this->result}</div><div class='clear'>&nbsp;</div>";
            }
        
        }else{
        
            foreach (
    $this->inputs as $v) {
            
            
    $v->editID=$this->editID;
            
                if(
    $v->fieldset){
                
                    if(
    $v->fieldset!=$fs){
                    
                        if(!
    $fs){
                        
    $output.=sprintf('<fieldset id="%s"><legend>%s</legend>',str_replace(' ','',$v->fieldset),$v->fieldset);
                        }else{
                        
    $output.=sprintf('</fieldset><fieldset id="%s"><legend>%s</legend>',str_replace(' ','',$v->fieldset),$v->fieldset);
                        }
                    
                    }
                
                }elseif(
    $fs){
                unset(
    $fs);
                
    $output.= '</fieldset>';
                }
            
                
    $output.="<div>".$v->get_input()."</div>\n";
            
                if(
    is_a($v,'file_input') || is_a($v,'image_input')){
                
    $enctype='enctype="multipart/form-data"';
                }
            
            
    $fs=$v->fieldset;
            
            }
        
        
    //if last loop was part of a fieldset, it needs to be closed
        
    if($fs){
        unset(
    $fs);
        
    $output.='</fieldset>';
        }
        
        
    printf ('<form %s method="%s" action="%s#user_result" id="%s"><p class="standards"><input type="hidden" name="norefresh" value="%s" /></p>',$enctype$this->method$this->action,$this->name,time());

        if(
    $this->result){
        echo 
    "<div id=\"user_result\">{$this->result}</div><div class='clear'>&nbsp;</div>";
        }
        
        print 
    $output."<p class='required-explantation'>(Required fields are marked with a <strong>*</strong>)</p>";
        
            if(!
    $this->noSubmit){
            
                if(
    $this->submitImage){
                print 
    "<div class=\"submit\"><input type='image' src='".$this->submitImage."' name='submit_{$this->name}' id='submit_{$this->name}' /></div>\n</form>\n";
                }else{
                print 
    "<div class=\"submit\"><input type='submit' value='".$this->submitText."' name='submit_{$this->name}' id='submit_{$this->name}' /></div>\n</form>\n";
                }
            
            }else{
            
            echo 
    "</form>";
            }
        
        }

    }

    //submit the form

    function submit(){

    if(
    $_SESSION['norefresh']==$_POST['norefresh']){
    $this->result="<p>Sorry, this form cannot be submitted by refreshing the browser.</p><p>Please submit using the button below.</p>";
    return 
    false;
    }
            
    global 
    $config;

        
    //hooks 
        
    if($this->editID){
        
            if(
    function_exists('before_new_hook')){
            @
    before_edit_hook();
            }
        
        }else{
            if(
    function_exists('before_new_hook')){
            @
    before_new_hook();
            }
        }
        
        for(
    $x=0;$this->inputs[$x];$x++){
        
            if(
    is_a($this->inputs[$x],'submit_input')){
            continue;
            }
            
        
    $this->inputs[$x]->editID=$this->editID;

            
    //process each field
            
            
    if(function_exists($this->inputs[$x]->process_function)){
            
                
    //need to use array call to pass by reference
                
    if(!call_user_func_array($this->inputs[$x]->process_function,array(&$this->inputs[$x]))){
                    if(
    $this->inputs[$x]->label){
                    
    $this->result.=sprintf('<li><a href="%s#%s">%s</a></li>',$this->action,$this->inputs[$x]->namestr_replace(':','',$this->inputs[$x]->label));
                    }
                
    $err=true;
                }
            
            }elseif(!
    $this->inputs[$x]->process()){
                if(
    $this->inputs[$x]->label){
                
    $this->result.=sprintf('<li><a href="%s#%s">%s</a></li>',$this->action,$this->inputs[$x]->namestr_replace(':','',$this->inputs[$x]->label));
                }

            
    $err=true;
            }
            
                
    //while there hasn't been an error, create the sql statement
                
    if(is_a($this->inputs[$x],'file_input') || is_a($this->inputs[$x],'image_input') || is_a($this->inputs[$x],'page') || is_a($this->inputs[$x],'relationship')|| is_a($this->inputs[$x],'captcha')){
                continue;
                }

                if(
    $this->inputs[$x]->fake){ //fake field so skipit
                
    continue;
                }
            
            if(!
    $err && $this->table && $this->primaryColumn){
                
                if(
    $this->editID){
                
                    if(
    is_a($this->inputs[$x],'date_input')){            
                    
    $sql .= $conj.$this->inputs[$x]->name."=""'".strftime('%Y-%m-%d',strtotime($_POST[$this->inputs[$x]->name]))."'";
                    }elseif(
    is_a($this->inputs[$x],'password_input')){        
                    
    $sql .= $conj.$this->inputs[$x]->name."=""AES_ENCRYPT('".cleaner($_POST[$this->inputs[$x]->name][0])."','{$config['aes']}')";
                    }else{
                    
    $sql .= $conj.$this->inputs[$x]->name."=""'".cleaner($_POST[$this->inputs[$x]->name])."'";
                    }
                
                
                }else{
    //adding to table
                
    $cols .= $conj."`".$this->inputs[$x]->name."`";
                
                    
    //convert date to timestamp
                    
    if(is_a($this->inputs[$x],'date_input')){        
                    
    //$vals .= $conj."'".strtotime($_POST[$this->inputs[$x]->name])."'";
                    
    $vals .= $conj."'".strftime('%Y-%m-%d',strtotime($_POST[$this->inputs[$x]->name]))."'";
                    }elseif(
    is_a($this->inputs[$x],'password_input')){        
                    
    $vals .= $conj."AES_ENCRYPT('".cleaner($_POST[$this->inputs[$x]->name][0])."','{$config['aes']}')";
                    }else{            
                    
    $vals .= $conj."'".cleaner($_POST[$this->inputs[$x]->name])."'";
                    }
                }

            
    $conj=", ";
            
            }
            
        }

                    
        
    //no error so do something with the collected data
        
    if(!$err){
                
        
            
    //does the form have a defined process function
            
            
    if(function_exists($this->process_function)){
                    
                
    $run=call_user_func($this->process_function);
                
                switch (
    $run){
                
                    case 
    true//process function completed successfuly & don't continue
                    
    $this->success=true;
                    return 
    true;
                    break;
                    
                    case 
    false//process function failed
                    
    return false;
                    break;
                
                }
        
            }
            
            
    //if user has specified a table add/edit the table
            
    if($this->table && $this->primaryColumn){
                        
                if(
    $this->editID){
            
                    if(
    $sql){ //if no columns, don't update (might just be taxo etc)
                
                        
    $sql "UPDATE ".$this->table." SET $sql WHERE ".$this->primaryColumn."=".$this->editID." LIMIT 1";
            
                            if(
    function_exists('before_update_hook')){
                            @
    before_update_hook($this->editID);
                            }
            
                        
    #bug fix 22/12/06
                        #returned if !update return false - if user just uploads files won't update table 
                        #better way: have date updated on all tables with images!
                        
                        
    $update=run_sql($sql);
                            
                        
    $config['debug_message']['info'][]="1 record from $this->table updated.";
                        
                        (
    $this->onUpdateResult $this->result=$this->onUpdateResult $this->result="<p>Record updated.</p>");
                    
                    }    
                                            
                    if(
    function_exists('after_update_hook')){
                    @
    after_update_hook($this->editID);
                    }
                                
                }else{
                
                
    $sql "INSERT INTO `".$this->table."` (`".$this->primaryColumn."`,$cols) VALUES ('',$vals)";
                
                
    $insert=run_sql($sql);
        
                    if (
    $insert){
                                
                    
    $config['debug_message']['info'][]="1 record inserted into ".$this->table;
                    
                    (
    $this->onAddResult $this->result=$this->onAddResult $this->result="<p>New item added.</p>");
                
                    
    $this->editID=mysql_insert_id();
                
                        
    //new record created
                        //run on new hook function, if it exists
                        
    if(function_exists('on_new_hook')){
                        @
    on_new_hook($this->editID);
                        }
        
                    }else{
                    
    $this->result="<p>Form error #314</p>";
                    return 
    false;
                    }

                }
                    
            }
            
            
    //files require the id of the item so process now    
            
    if($this->editID){
                
    //(is_numeric($this->editID) ? $id=$this->editID : $id=$newID);
                            
                
    foreach ($this->inputs as $v) {
                
                    if(
    method_exists($v,'on_completion')){
                    
    $v->on_completion($this->editID);
                    }
                    
                }            
            }
            
    //if user has requested email notification
            
    if(is_array($this->notify)){

                if(
    $this->htmlMessageFormat){
                
                    if(!
    $this->plainTextMessageFormat){
                    
    $plain=$this->htmlMessageFormat;
                    }
                
                
    $html=$this->htmlMessageFormat;
                
                }
                
                if(
    $this->plainTextMessageFormat){
                
                    if(!
    $this->htmlMessageFormat){
                    
    $html=$this->plainTextMessageFormat;
                    }
                
                
    $plain=$this->plainTextMessageFormat;
                
                }
                
                if(!
    $html && !$plain){
                
                    foreach (
    $this->inputs as $v) {
                    
    $plain.=$_POST[$v->name]."\r\n";
                    
    $html.=$_POST[$v->name]."<br />";
                    }
                
                }
                
                foreach (
    $this->notify as $to){
                    
                
    $send=send_html_email($to,$this->messageFrom,$this->messageSubject,$plain,$html);
        
                    if(!
    $send[0]){
                    
    $this->result="<p>Sorry, we were unable to send email at this time.</p>";
                    return 
    false;
                    }
                
                }
                
                if(
    $this->onSendResult){
                
    $this->result=$this->onSendResult;
                }
                
            }
        
            if(
    $this->clear){
            
    $_POST='';
            }

        
    //if got to here then all is cool
        
        
    $this->success=true;
        
    $_SESSION['norefresh']=$_POST['norefresh'];
            
        }else{ 
    //error
        
            
    if($this->result){
            
    $this->result="<p>Sorry, some required information is missing or incorrect. Errors were found with the following fields:</p> <ul>{$this->result}</ul><p>Please check the form and try again.</p>";
            }
        }

    }

    function 
    populate_data(){

    global 
    $config;

        foreach (
    $this->inputs as $f) {
        
        if(
    is_a($f,'relationship') || is_a($f,'file_input') || is_a($f,'image_input') || is_a($f,'submit_input')){
        continue;
        }elseif(
    is_a($f,'page')){
        
        
    $file=$config['root_path'].'/'.$config['page_dir'].'/'.$this->editID.'/index.php';
        
            if(@
    is_file($file)){    
            
    $_POST[$f->name]=file_get_contents($file);
            }
            
        continue;
        }
        
            if(!
    $f->fake){ //fake inputs are not part of a table
            
    $cols.=$conj.$f->name
            
    $conj ",";
            }
        
        }

    if(!
    $cols){
    return 
    false;
    }

    $sql="SELECT " $cols ." FROM ".$this->table." WHERE ".$this->primaryColumn."=".$this->editID." LIMIT 1";

    $result=run_select_sql($sql);

        if(
    $result){
        
        
    $data mysql_fetch_assoc($result);
        
            if(
    $data){
            
                foreach (
    $data as $k=>$d){
                
    $_POST[$k]=($d);
                } 
            
            }
        
        }    


    }

    //add some events
    function activate(){
        
    global 
    $config;

        if(
    $_POST['submit_'.$this->name] || is_numeric($_POST['submit_'.$this->name.'_x'])){

            if(!
    $config['local'] && $config['domain_name']!=str_replace('www.','',$_SERVER['HTTP_HOST'])){
            die(
    'Error. Please contact site admin: '.$config['admin']);
            }
        
            
    $this->submit();
        
        }elseif(
    $this->editID && $this->populate){
        
    $this->populate_data();
        }

        
    $this->output_form();

    }

    }
    //end of class 

  4. #4
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is OOP so probably best answered in the php OOP forum - personally I don't really understand your code.

    Having said that, why don't you just delete the anchor from the header(), and show the error at the top of the page, instead of jumping to an anchor?
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  5. #5
    SitePoint Member
    Join Date
    May 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey wheeler -

    You're right: that's a much, much better way of doing it. I'm just making it all too complicated when there's a nice, simple solution staring me in the face - thanks for pointing it out.

    Cheers, 83n

  6. #6
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no probs, next time you see me doing things harder than they need to be, make sure u tell me straight up too! I think we are all guilty of over complicating things from time to time
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development


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
  •