Solved

Open A New Window Using PHP

Posted on 2004-08-15
39
373 Views
Last Modified: 2008-02-01
Ok the script is below what i need to happen is when the click submit it opens the url in a new window is this posible:

<?php
  if (isset($$payment->form_action_url)) {
    $form_action_url = $$payment->form_action_url;
  } else {
    $form_action_url = tep_href_link(FILENAME_CHECKOUT_PROCESS, '', 'SSL');
  }

  echo tep_draw_form('checkout_confirmation', $form_action_url, 'post');

  if (is_array($payment_modules->modules)) {
    echo $payment_modules->process_button();
  }

  echo tep_image_submit('button_confirm_order.gif', IMAGE_BUTTON_CONFIRM_ORDER) . '</form>' . "\n";
?>

Cheers
0
Comment
Question by:Tank400
  • 18
  • 18
39 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 11804558
in the form line add

target="_blank"

eg.

<form action="page.php" method="post" name="form" target="_blank">
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11804562
by the looks of your code you will need to edit the line that echos/prints the form line in the tep_draw_form function
0
 
LVL 1

Author Comment

by:Tank400
ID: 11804564
how do i do that there is no for the form bit is written in php
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 27

Expert Comment

by:Diablo84
ID: 11804574
can you show me the function tep_draw_form
0
 
LVL 1

Author Comment

by:Tank400
ID: 11804578
ok would it be posible for you to rewrite that script so that it does what it needs to do i would be really greatfull if you could
0
 
LVL 1

Author Comment

by:Tank400
ID: 11804607
i dont know what the function tep_draw_form i think it is something that is creating the form
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11804623
yeah, within it it will have a line that will resemble something like

echo "<form name=\"$name\" action=\"$action\" method=\"$method\">";

you need to add target="_blank" in that line, eg.

echo "<form name=\"$name\" action=\"$action\" method=\"$method\" target=\"_blank\">";

The function will either be at the top of the file somewhere, or prehaps in an include file
0
 
LVL 1

Author Comment

by:Tank400
ID: 11804628
ok i will see what i can find show u cant just add something to that script
0
 
LVL 1

Author Comment

by:Tank400
ID: 11804636
i cant seem to find the line anywhere
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11804645
if there is no parameter in the function to handle the target (which it does not appear to have) then you will need to directly edit the functions code to account for it.

I suspect that you will have a file storing a range of functions which is included in the script.

you need to find a line which starts

function tep_draw_form ($parameters....) {

look for any includes on your page

ie. include(filename.php'); and see if the function is in one of those files
0
 
LVL 1

Author Comment

by:Tank400
ID: 11804650
ok i think i have found it but if i ad that line to it. every form on the site will open in a new window
0
 
LVL 1

Author Comment

by:Tank400
ID: 11804655
ok this is what i have found

// Output a form
  function tep_draw_form($name, $action, $method = 'post', $parameters = '') {
    $form = '<form name="' . tep_output_string($name) . '" action="' . tep_output_string($action) . '" method="' . tep_output_string($method) . '"';

    if (tep_not_null($parameters)) $form .= ' ' . $parameters;

    $form .= '>';

    return $form;
  }

////
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11804670
ok heres what were going to have to do, add an extra param to the function so you have the option of setting the target

the function:

// Output a form
  function tep_draw_form($name, $action, $method = 'post', $parameters = '' $target = '_self') {
    $form = '<form name="' . tep_output_string($name) . '" target="' . tep_output_string($target) . '" action="' . tep_output_string($action) . '" method="' . tep_output_string($method) . '"';

    if (tep_not_null($parameters)) $form .= ' ' . $parameters;

    $form .= '>';

    return $form;
  }

////

then your code:

<?php
  if (isset($$payment->form_action_url)) {
    $form_action_url = $$payment->form_action_url;
  } else {
    $form_action_url = tep_href_link(FILENAME_CHECKOUT_PROCESS, '', 'SSL');
  }

  echo tep_draw_form('checkout_confirmation', $form_action_url, 'post','','_blank');

  if (is_array($payment_modules->modules)) {
    echo $payment_modules->process_button();
  }

  echo tep_image_submit('button_confirm_order.gif', IMAGE_BUTTON_CONFIRM_ORDER) . '</form>' . "\n";
?>

i think that should do the trick, give it a try and let me know
0
 
LVL 1

Author Comment

by:Tank400
ID: 11804675
yeah its doing just what i said every form opens in a new window
0
 
LVL 1

Author Comment

by:Tank400
ID: 11804691
hi thanks for that added it but i now get a error

Parse error: parse error, unexpected T_VARIABLE, expecting ')' in W:\includes\functions\html_output.php on line 148
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11804701
in the function

replace:

function tep_draw_form($name, $action, $method = 'post', $parameters = '' $target = '_self') {

with:

function tep_draw_form($name, $action, $method = 'post', $parameters = '', $target = '_self') {
0
 
LVL 1

Author Comment

by:Tank400
ID: 11804708
ok i dnt get an error mesasge but the page dosent pop up either
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11804716
hmm, can you show me the output of the form (ie. what you see in the browser)

right click - view source then copy the line for the form

<form name=" .... etc

I want to check the output of the function so i can see whats going wrong
0
 
LVL 1

Author Comment

by:Tank400
ID: 11804820
ok this is the form bit:

<form name="checkout_confirmation" action="https://secure.paypal.com/cgi-bin/webscr" method="post">
<input type="image" src="includes/languages/english/images/buttons/button_confirm_order.gif" border="0" alt="Confirm Order" title=" Confirm Order "></form>
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11804845
hmm its not even including the target, have you definitely updated the function?

0
 
LVL 1

Author Comment

by:Tank400
ID: 11805094
yep this is what i have on the include page:

<?php
////
// The HTML href link wrapper function
  function tep_href_link($page = '', $parameters = '', $connection = 'NONSSL', $add_session_id = true, $search_engine_safe = true) {
    global $request_type, $session_started, $SID;

    if (!tep_not_null($page)) {
      die('</td></tr></table></td></tr></table><br><br><font color="#ff0000"><b>Error!</b></font><br><br><b>Unable to determine the page link!<br><br>');
    }

    if ($connection == 'NONSSL') {
      $link = HTTP_SERVER . DIR_WS_HTTP_CATALOG;
    } elseif ($connection == 'SSL') {
      if (ENABLE_SSL == true) {
        $link = HTTPS_SERVER . DIR_WS_HTTPS_CATALOG;
      } else {
        $link = HTTP_SERVER . DIR_WS_HTTP_CATALOG;
      }
    } else {
      die('</td></tr></table></td></tr></table><br><br><font color="#ff0000"><b>Error!</b></font><br><br><b>Unable to determine connection method on a link!<br><br>Known methods: NONSSL SSL</b><br><br>');
    }

    if (tep_not_null($parameters)) {
      $link .= $page . '?' . tep_output_string($parameters);
      $separator = '&';
    } else {
      $link .= $page;
      $separator = '?';
    }

    while ( (substr($link, -1) == '&') || (substr($link, -1) == '?') ) $link = substr($link, 0, -1);

// Add the session ID when moving from different HTTP and HTTPS servers, or when SID is defined
    if ( ($add_session_id == true) && ($session_started == true) && (SESSION_FORCE_COOKIE_USE == 'False') ) {
      if (tep_not_null($SID)) {
        $_sid = $SID;
      } elseif ( ( ($request_type == 'NONSSL') && ($connection == 'SSL') && (ENABLE_SSL == true) ) || ( ($request_type == 'SSL') && ($connection == 'NONSSL') ) ) {
        if (HTTP_COOKIE_DOMAIN != HTTPS_COOKIE_DOMAIN) {
          $_sid = tep_session_name() . '=' . tep_session_id();
        }
      }
    }

    if ( (SEARCH_ENGINE_FRIENDLY_URLS == 'true') && ($search_engine_safe == true) ) {
      while (strstr($link, '&&')) $link = str_replace('&&', '&', $link);

      $link = str_replace('?', '/', $link);
      $link = str_replace('&', '/', $link);
      $link = str_replace('=', '/', $link);

      $separator = '?';
    }

    if (isset($_sid)) {
      $link .= $separator . $_sid;
    }

    return $link;
  }

////
// The HTML image wrapper function
  function tep_image($src, $alt = '', $width = '', $height = '', $parameters = '') {
    if ( (empty($src) || ($src == DIR_WS_IMAGES)) && (IMAGE_REQUIRED == 'false') ) {
      return false;
    }

// alt is added to the img tag even if it is null to prevent browsers from outputting
// the image filename as default
    $image = '<img src="' . tep_output_string($src) . '" border="0" alt="' . tep_output_string($alt) . '"';

    if (tep_not_null($alt)) {
      $image .= ' title=" ' . tep_output_string($alt) . ' "';
    }

    if ( (CONFIG_CALCULATE_IMAGE_SIZE == 'true') && (empty($width) || empty($height)) ) {
      if ($image_size = @getimagesize($src)) {
        if (empty($width) && tep_not_null($height)) {
          $ratio = $height / $image_size[1];
          $width = $image_size[0] * $ratio;
        } elseif (tep_not_null($width) && empty($height)) {
          $ratio = $width / $image_size[0];
          $height = $image_size[1] * $ratio;
        } elseif (empty($width) && empty($height)) {
          $width = $image_size[0];
          $height = $image_size[1];
        }
      } elseif (IMAGE_REQUIRED == 'false') {
        return false;
      }
    }

    if (tep_not_null($width) && tep_not_null($height)) {
      $image .= ' width="' . tep_output_string($width) . '" height="' . tep_output_string($height) . '"';
    }

    if (tep_not_null($parameters)) $image .= ' ' . $parameters;

    $image .= '>';

    return $image;
  }

////
// The HTML form submit button wrapper function
// Outputs a button in the selected language
  function tep_image_submit($image, $alt = '', $parameters = '') {
    global $language;

    $image_submit = '<input type="image" src="' . tep_output_string(DIR_WS_LANGUAGES . $language . '/images/buttons/' . $image) . '" border="0" alt="' . tep_output_string($alt) . '"';

    if (tep_not_null($alt)) $image_submit .= ' title=" ' . tep_output_string($alt) . ' "';

    if (tep_not_null($parameters)) $image_submit .= ' ' . $parameters;

    $image_submit .= '>';

    return $image_submit;
  }

////
// Output a function button in the selected language
  function tep_image_button($image, $alt = '', $parameters = '') {
    global $language;

    return tep_image(DIR_WS_LANGUAGES . $language . '/images/buttons/' . $image, $alt, '', '', $parameters);
  }

////
// Output a separator either through whitespace, or with an image
  function tep_draw_separator($image = 'pixel_black.gif', $width = '100%', $height = '1') {
    return tep_image(DIR_WS_IMAGES . $image, '', $width, $height);
  }

////
// Output a form
  function tep_draw_form($name, $action, $method = 'post', $parameters = '' $target = '_self') {
    $form = '<form name="' . tep_output_string($name) . '" target="' . tep_output_string($target) . '" action="' . tep_output_string($action) . '" method="' . tep_output_string($method) . '"';

    if (tep_not_null($parameters)) $form .= ' ' . $parameters;

    $form .= '>';

    return $form;
  }

////
// Output a form input field
  function tep_draw_input_field($name, $value = '', $parameters = '', $type = 'text', $reinsert_value = true) {
    $field = '<input type="' . tep_output_string($type) . '" name="' . tep_output_string($name) . '"';

    if ( (isset($GLOBALS[$name])) && ($reinsert_value == true) ) {
      $field .= ' value="' . tep_output_string(stripslashes($GLOBALS[$name])) . '"';
    } elseif (tep_not_null($value)) {
      $field .= ' value="' . tep_output_string($value) . '"';
    }

    if (tep_not_null($parameters)) $field .= ' ' . $parameters;

    $field .= '>';

    return $field;
  }

////
// Output a form password field
  function tep_draw_password_field($name, $value = '', $parameters = 'maxlength="40"') {
    return tep_draw_input_field($name, $value, $parameters, 'password', false);
  }

////
// Output a selection field - alias function for tep_draw_checkbox_field() and tep_draw_radio_field()
  function tep_draw_selection_field($name, $type, $value = '', $checked = false, $parameters = '') {
    $selection = '<input type="' . tep_output_string($type) . '" name="' . tep_output_string($name) . '"';

    if (tep_not_null($value)) $selection .= ' value="' . tep_output_string($value) . '"';

    if ( ($checked == true) || ( isset($GLOBALS[$name]) && is_string($GLOBALS[$name]) && ( ($GLOBALS[$name] == 'on') || (isset($value) && (stripslashes($GLOBALS[$name]) == $value)) ) ) ) {
      $selection .= ' CHECKED';
    }

    if (tep_not_null($parameters)) $selection .= ' ' . $parameters;

    $selection .= '>';

    return $selection;
  }

////
// Output a form checkbox field
  function tep_draw_checkbox_field($name, $value = '', $checked = false, $parameters = '') {
    return tep_draw_selection_field($name, 'checkbox', $value, $checked, $parameters);
  }

////
// Output a form radio field
  function tep_draw_radio_field($name, $value = '', $checked = false, $parameters = '') {
    return tep_draw_selection_field($name, 'radio', $value, $checked, $parameters);
  }

////
// Output a form textarea field
  function tep_draw_textarea_field($name, $wrap, $width, $height, $text = '', $parameters = '', $reinsert_value = true) {
    $field = '<textarea name="' . tep_output_string($name) . '" wrap="' . tep_output_string($wrap) . '" cols="' . tep_output_string($width) . '" rows="' . tep_output_string($height) . '"';

    if (tep_not_null($parameters)) $field .= ' ' . $parameters;

    $field .= '>';

    if ( (isset($GLOBALS[$name])) && ($reinsert_value == true) ) {
      $field .= stripslashes($GLOBALS[$name]);
    } elseif (tep_not_null($text)) {
      $field .= $text;
    }

    $field .= '</textarea>';

    return $field;
  }

////
// Output a form hidden field
  function tep_draw_hidden_field($name, $value = '', $parameters = '') {
    $field = '<input type="hidden" name="' . tep_output_string($name) . '"';

    if (tep_not_null($value)) {
      $field .= ' value="' . tep_output_string($value) . '"';
    } elseif (isset($GLOBALS[$name])) {
      $field .= ' value="' . tep_output_string(stripslashes($GLOBALS[$name])) . '"';
    }

    if (tep_not_null($parameters)) $field .= ' ' . $parameters;

    $field .= '>';

    return $field;
  }

////
// Hide form elements
  function tep_hide_session_id() {
    global $session_started, $SID;

    if (($session_started == true) && tep_not_null($SID)) {
      return tep_draw_hidden_field(tep_session_name(), tep_session_id());
    }
  }

////
// Output a form pull down menu
  function tep_draw_pull_down_menu($name, $values, $default = '', $parameters = '', $required = false) {
    $field = '<select name="' . tep_output_string($name) . '"';

    if (tep_not_null($parameters)) $field .= ' ' . $parameters;

    $field .= '>';

    if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);

    for ($i=0, $n=sizeof($values); $i<$n; $i++) {
      $field .= '<option value="' . tep_output_string($values[$i]['id']) . '"';
      if ($default == $values[$i]['id']) {
        $field .= ' SELECTED';
      }

      $field .= '>' . tep_output_string($values[$i]['text'], array('"' => '&quot;', '\'' => '&#039;', '<' => '&lt;', '>' => '&gt;')) . '</option>';
    }
    $field .= '</select>';

    if ($required == true) $field .= TEXT_FIELD_REQUIRED;

    return $field;
  }

////
// Creates a pull-down list of countries
  function tep_get_country_list($name, $selected = '', $parameters = '') {
    $countries_array = array(array('id' => '', 'text' => PULL_DOWN_DEFAULT));
    $countries = tep_get_countries();

    for ($i=0, $n=sizeof($countries); $i<$n; $i++) {
      $countries_array[] = array('id' => $countries[$i]['countries_id'], 'text' => $countries[$i]['countries_name']);
    }

    return tep_draw_pull_down_menu($name, $countries_array, $selected, $parameters);
  }
?>

and on the page where the button is:

<?php
  if (isset($$payment->form_action_url)) {
    $form_action_url = $$payment->form_action_url;
  } else {
    $form_action_url = tep_href_link(FILENAME_CHECKOUT_PROCESS, '', 'SSL');
  }

  echo tep_draw_form('checkout_confirmation', $form_action_url, 'post','','_blank');

  if (is_array($payment_modules->modules)) {
    echo $payment_modules->process_button();
  }

  echo tep_image_submit('button_confirm_order.gif', IMAGE_BUTTON_CONFIRM_ORDER) . '</form>' . "\n";
?>
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11805135
hmm, i cant understand why its leaving out the target, prehaps a caching issue, try pressing ctrl + f5 on the form page to do a complete refresh

as a temp fix if that doesnt do it change your code (not the function) to this, to create the form without the function. It will work just as well, if not more efficently and will do until i can figure out why its not printing out the target:

<?php
  if (isset($$payment->form_action_url)) {
    $form_action_url = $$payment->form_action_url;
  } else {
    $form_action_url = tep_href_link(FILENAME_CHECKOUT_PROCESS, '', 'SSL');
  }

  echo "<form name=\"checkout_confirmation\" action=\"https://secure.paypal.com/cgi-bin/webscr\" method=\"post\" target=\"_blank\">";

  if (is_array($payment_modules->modules)) {
    echo $payment_modules->process_button();
  }

  echo tep_image_submit('button_confirm_order.gif', IMAGE_BUTTON_CONFIRM_ORDER) . '</form>' . "\n";
?>
0
 
LVL 1

Author Comment

by:Tank400
ID: 11805180
right i have just tested it for a seconds time at it seems to work you are a genuis only this that would be cool is if after the other window popups the orignal page could go to another is this posible
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11805198
not with php or html alone, i think it may be possible with javascript but it will mean more function editing.

Il have to look into this one, javascript isnt my strong point.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11805218
ok im not too sure to be frank, at this moment i cant get the idea i had to work and as i said JS is not my strong point

It might be worth while asking a new question in the javascript topic area about this one.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11805236
This is as far as i could get with it, but im not sure if it will work, you can test it if you like but no promises on this one

add this to the top of your page with the code in

<script language="JavaScript">
function changeloc() {
window.location.href = 'another_page.htm';
}
</script>

and change another_page.htm to the page you want to display

then change your code to:

<?php
  if (isset($$payment->form_action_url)) {
    $form_action_url = $$payment->form_action_url;
  } else {
    $form_action_url = tep_href_link(FILENAME_CHECKOUT_PROCESS, '', 'SSL');
  }

  echo tep_draw_form('checkout_confirmation', $form_action_url, 'post','','_blank');

  if (is_array($payment_modules->modules)) {
    echo $payment_modules->process_button();
  }
 
  echo "<input type=\"image\" src=\"includes/languages/english/images/buttons/button_confirm_order.gif\" border=\"0\" alt=\"Confirm Order\" title=\" Confirm Order \" onClick=\"changeloc()\">\n";
  echo "</form>";
?>

notice i abandoned using your button function for the sole reason that it would have caused more complication then i feel is necessary.
0
 
LVL 48

Expert Comment

by:hernst42
ID: 11805248
You can change the url of the parent-window if you insert code like the following into the child-window (popup):

<script language="JavaScript" type="text/javascript">
window.opener.location.href = 'the new URL';
</script>

If you have no control of the output of the opend window you may first create a new window via JS and store the handle in a JS-Variable.
Use the name of the opend Window as target in that form. Then use a function that check every second if that opend-window still exits and which URL ist has.
If a certain criteria matches (e.g Window closed, specific URL) set the url of the parent to the new url.
0
 
LVL 1

Author Comment

by:Tank400
ID: 11805272
yep it kinda works it does exactly what i wot but there are to erros on the top of the page:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at W:\checkout_confirmation.php:1) in W:\includes\functions\sessions.php on line 67

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at W:\checkout_confirmation.php:1) in W:\includes\functions\sessions.php on line 67
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11805280
session_start(); must go before any output so put the function after that.
0
 
LVL 1

Author Comment

by:Tank400
ID: 11805286
do you mean

<script language="JavaScript" type="text/javascript">
window.opener.location.href = 'the new URL';
</script>
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11805295
if that is the javascript you are using on the page yes.

Just make sure that on the page that generated the error session_start(); is at the top of the page before any other code.
0
 
LVL 1

Author Comment

by:Tank400
ID: 11805308
ok its seems to be working ok atm but I will need to double check stuff thanks so much so far for all your help I will let you know how I get on

Thanks Again
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11805314
quite alright, good luck with it :)

|)iablo
0
 
LVL 1

Author Comment

by:Tank400
ID: 11805337
hi im just configuring the paypal lineI need it to be the folowing but i dont completely now how to write the line:

This what it is like atm:
 tep_draw_hidden_field('cancel_return', tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));

This is kind of what i want basicly so it will return to a different url if you know what i mean
 tep_draw_hidden_field('cancel_return', tep_href_link(some url in here, '', 'SSL'));
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11805373
the problem with keep editing your functions is it could cause problems elsewhere on your script and further more i just had a look at the tep_href_link function which would require updating to do this and its a pretty hefty chunk of code which frankly i would avoid tampering with.

To summerise, if i was you i would just output the code direct bypassing the function.

eg.

echo "<input type=\"hidden\" name=\"cancel_return\" value=\"value content\">";

without seeing the function in action im not fully sure of the output of FILENAME_CHECKOUT_PAYMENT, '', 'SSL' so if you run it once, look at the output and then stick it in the above code where "value content" is it should do the job fine.
0
 
LVL 1

Author Comment

by:Tank400
ID: 11805381
i have managed to work around that problem I am trying to get a page to automaticly close as soon as someone hits it
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 500 total points
ID: 11805393
in your body tag (<body>) replace it with

<body onLoad="javascript:window.close();">
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question