• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
form submitted data into new window

#1
Hi,
can anyone know how to do this in a easy way?

when I press the submit button of a form, if the data added successfully into database, then same time how to open a new window of template with the submitted data values?

I want to know how to pass the form data as parameter into  window.open();
Reply

#2
Way #1:
Change the button to a normal (non-submitting) button.
Grab the variables from the form using JavaScript (jQuery...)
Assemble the URL for the new window with the elements from the form
Do a window.open command with the variables appended to the URL
Then submit the form.

Way #2:
Change the button to a normal (non-submitting) button.
AJAX submit the data to your server.
Have the server respond with the ID from the record back to the page.
On a successful write, use the window.open command with the ID on it so the page will open the exact record and load up the details

If you're also having the page with a form go somewhere else, do that after all of the above is done with a window.href command.
Reply

#3
I'm new to this thing and little bit difficult when i try to do this. I want to do this according to your Way #2:
This is my source code and its very simple, but confuse how and where to add those few new lines you mention in Way #2:.
Can you please add those few line in the relevant places.

After submit data, I need to open a new Window/Tab of template letter with the submitted data values embedded on that letter to print it.


//////////////VIEW/////////////////

<div class="messages"></div>   
<!--******************** START SESSION SETFLASH MESSAGES *****************************-->         
         <?php if($this->session->flashdata('message')){?>  
        <div class="alert alert-success alert-dismissible" role="alert">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <strong> <span class="glyphicon glyphicon-ok-sign"></span> </strong><?php echo $this->session->flashdata('message')?>
    </div>
         <?php } ?>                  
<!--************************* END SESSION SETFLASH MESSAGES   ************************-->
            

 <form action="#" class="form-horizontal" id="form_new_user"> 
  
 <div class="form-group">
     <label class="col-sm-2 control-label">Username</label>
     <div class="col-sm-2" ><label> <input type="text" name="username" class="form-control" required/><span class="help-block"></span> </label></div>
 </div>
                                                                          
 <div class="form-group">  
     <label class="col-sm-2 control-label">Display Name</label>
     <div class="col-sm-3" ><label><input type="text" name="name" class="form-control" required/><span class="help-block"></span> </label></div>
 </div>
 
 <div class="form-group">
 <label class="col-sm-2 control-label">Password</label>
 <div class="col-sm-2" ><label><input type="password" name="password" class="form-control" required/><span class="help-block"></span> </label></div>
 </div>
    
<div class="form-group">
 <label class="col-sm-2 control-label">Confirm Password</label>
 <div class="col-sm-2" ><label><input type="password" name="password_confirm" class="form-control" required/><span class="help-block"></span> </label></div>
</div>
     
<div class="form-group">
 <label class="col-sm-2 control-label">User Type</label>
 <div class="col-sm-2" ><label>
     <select name="user_type" id="user_type" class="form-control" required>      
        <option value="-1"> Select User Type</option>      
        <?php foreach($info_user_type_notAdmin as $row) { ?>      
<option value="<?php echo $row->type_id?>" > <?php echo $row->type_name?></option>  
            <?php } ?>
              
 </select><span class="help-block"></span> </label>
 </div>
</div>
     
<div class="form-group">
 <label class="col-sm-2 control-label">Email</label>
 <div class="col-sm-2" ><label><input type="text" name="email" class="form-control" /><span class="help-block"></span> </label></div>
</div>
                                 
<br>

<div class="form-group">
    <label class="col-sm-4 control-label">
          <button type="button" class="btn-success btn-sm" id="btn_add_emp" onClick="add_user()"> &nbsp;ADD</button>
          <button type="button" class="btn-danger btn-sm" id="btn_add_cancel"> &nbsp;CANCEL</button>
    </label>
</div>

</form>


////////////////AJAX FUNCTION////////////

function add_user()
    {             
          $.ajax({
            url : "<?php echo site_url('index.php/Admin/add_new_user')?>",      
            type: "POST",
            data: $('#form_new_user').serialize(),
            dataType: "JSON",
            
            success: function(data)
            {
                 if(data.status)
                        {              
                           location.reload();
                        }
                         else
                        {
                            for (var i = 0; i < data.inputerror.length; i++)
                            {
$('[name="'+data.inputerror[i]+'"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
$('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]); //select span help-block class set text error string
                            }
                        }
            },
            error: function (jqXHR, textStatus, errorThrown)
            {
                 //how to display red coloured error msg when selected role already assigned
                alert('ERROR in ADDING NEW USER');
            }
        });     
    }

/////////////////////CONTROLLER FUNCTION////////////////
public function add_new_user() {
            $this->_validate();

            date_default_timezone_set('Europe/London');//get current time at register_date
            $date_time_now = date('Y-m-d   h:iConfused a', time());
              
            $data = array(    
            'username' => $this->input->post('username'),
            'name' => $this->input->post('name'),
            'password' => $this->input->post('password'),
            'email' => $this->input->post('email'),
            'register_date' => $date_time_now,
            'validate'  =>1,
            'type_id' => 2
        );        
         $this->load->model('Model_user');
         $insert = $this->Model_user->model_add_new_user($data);
         $this->session->set_flashdata('message', 'Your data inserted Successfully......');         
     echo json_encode(array("status" => TRUE));  
        }

/////////////MODEL FUNCTION/////////////
 public function model_add_new_user($data)
        {  
        $this->db->insert('user_login', $data);
        return $this->db->insert_id();
        }
Reply

#4
In your controller:
PHP Code:
   $this->load->model('Model_user');
 
   $insert $this->Model_user->model_add_new_user($data);
 
   echo json_encode(array("status" => TRUE,"ID"=>$insert));   
In your AJAX call:
Code:
success: function(data)
           {
                if(data.status)
                       {
                          window.open("[someURLhere]/"+data.ID);
                          location.reload();
                       }

Change out the [someURLhere] with your url and any parameters you need (plus the rest of the open command, as you need for your new window)

In the receiving URL, then, you'd have the controller be something like:

PHP Code:
function show_data($id) {
 
 // use the $id to lookup the data via your model
 
 // do stuff here with the ID and display it.

Reply

#5
Your instructions works perfectly.
I did idiot work as I set My browser popup block.

Now its ok. thank you for your guidance. Smile
Reply

#6
By the way, how to print the WHOLE html page that is open in new window. That is print VIEW in codeigniter.
I did it normal php like this. It is not working for codeigniter.
What is the equilent method in codeigniter? 
How do we do it? Can anyone please help me?
datatables provide print option to print table of content, but here no table to print. it is not the way Sad
///////////////
<form name="my_form" action="<?php echo ($SERVER['PHP_SELF']);?>" method="post">
  <input type="submit" name="submit_docs" value="Export as MS Word" />
</form>

<?php
if (isset($_POST['submit_docs'])) {
header("Content-type: application/vnd.ms-word");
header("Expires: 0");//no-cache
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");//no-cache
header("Content-Disposition: attachment;Filename=document_name.doc");
}
///html content suppose to print goes here
echo"<html>";
etc....
Reply

#7
You should start a new topic as that is a new question.
Also, use the PHP tag on your code ([php]) so your PHP code will look like mine.  You can preview your post and use the icons to the right of the toolbar to insert those tags for you.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.