2014-07-23 10 views
13

Ich habe eine Javascript-Variable namens "list". Ich muss senden Sie es als POST-Daten auf eine andere Seite und öffnen Sie diese Seite in einem neuen Tab (mit den POST-Daten vorhanden).jQuery Seite in einem neuen Tab öffnen, während POST-Daten übergeben werden

Dieser Code:

jQuery.post('datadestination.php', list); 

sendet die Daten in Ordnung, aber natürlich öffnet es die Seite in der gleichen Registerkarte.

Ich sah einige Lösungen für ähnliche Probleme mit unsichtbarer Form und solche Dinge, aber ich konnte sie nicht zur Arbeit bringen. Gibt es eine einfache Lösung?

+1

Die Lösung in http beschrieben hinzu: //stackoverflow.com/questions/7024040/jquery-open-page-in-a-tab-and-pass-some-post-values ​​scheint zu funktionieren. Haben Sie spezielle Probleme damit? Was geschieht? – LSerni

+1

Sie können auch eine JavaScript-Funktion aufrufen, die dynamisch ein Formular mit einem 'target = '_ blank'-Attribut erstellt: http://stackoverflow.com/questions/7013109/submit-is-not-a-function-error-in- firefox-in-dynamically-created-form-without – Stefan

Antwort

11

1) Warum diese Liste nicht mit der jquery.post() Funktion übergeben und sie im SESSION Array speichern;

2) Dann öffnen Sie eine neue Registerkarte mit der gleichen Datei/Adresse/URL mit der window.open() Funktion;

3) Abrufen gespeicherter Daten aus dem Array SESSION;

Scheint eine einfache und saubere Art und Weise?

+0

Das ist natürlich brillant ... Irgendwie konnte ich nicht mit dieser unsichtbaren Form darbringen. – zorza

11

Sie können ein Formular mit dem Attribut target = "_ blank" senden.

<form action="datadestination.php" method="POST" target="_blank" id="myform"> 
    <input type="hidden" name="list" id="list-data"/> 
    <input type="submit" value="Submit"> 
</form> 

dann in JS:

jQuery('#list-data').val(list); 
jQuery('#myform').submit(); 
+0

Es öffnet eine zusätzliche leere Registerkarte jede Idee, was der Grund sein könnte? –

13

Dies ist eine Implementierung von Sergey Lösung.

<?php // this is save.php 
    session_start(); 
    // DO NOT just copy from _POST to _SESSION, 
    // as it could allow a malicious user to override security. 
    // Use a disposable variable key, such as "data" here. 
    // So even if someone passed _POST[isAdmin]=true, all that he would do 
    // is populate _SESSION[data][isAuthenticated], which nobody reads, 
    // not the all-important _SESSION[isAuthenticated] key. 
    if (array_key_exists('data', $_POST)) { 
     $_SESSION['data']    = $_POST['data']; 
     $_SESSION['data.timestamp'] = time(); 
     // Let us let the client know what happened 
     $msg = 'OK'; 
    } else { 
     $msg = 'No data was supplied'; 
    } 
    Header('Content-Type: application/json; charset=utf8'); 
    die(json_encode(array('status' => $msg))); 
?> 

auf der ersten Seite:

$.post('save.php', { data: list }, function(response){ 
    if (!response.status) { 
     alert("Error calling save"); 
     return; 
    } 
    if (response.status !== 'OK') { 
     alert(response.status); 
     return; 
    } 
    // We had a response and it was "OK". We're good. 
    window.open('datadestination.php'); 
}); 

Und in datadestination.php fügen Sie den fix:

if (!array_key_exists('data', $_SESSION)) { 
    die("Problems? Did you perchance attempt to reload the page and resubmit?"); 
    // For if he did, then yes, $_SESSION would have been cleared. 

    // Same if he is operating on more than one window or browser tab. 
} 
// Do something to validate data. For example we can use data.timestamp 
// to assure data isn't stale. 
$age = time(); 
if (array_key_exists($ts = 'data.timestamp', $_SESSION)) { 
    $age -= $_SESSION[$ts]; 
} 
if ($age > 3600) { 
    die("Data is more than one hour old. Did someone change server time?!?"); 
    // I actually had ${PFY} do that to me using NTP + --hctosys, once. 
    // My own time zone is (most of the year) exactly one hour past GMT. 
} 

// This is safe (we move unsecurity-ward): 
$_POST = $_SESSION['data']; 
unset($_SESSION['data'], $_SESSION['data.timestamp']); 
// keep things clean. 

// From here on, the script behaves "as if" it got a _POST. 

aktualisieren

Sie können tatsächlich mergesave.php und datadestination.php und Verwenden Sie einen "Sicherungs-Stub" savepost.php, die Sie auf anderen Seiten recyceln:

<?php 
    session_start(); 

    // DO NOT just copy from _POST to _SESSION, 
    // as it could allow a malicious user to override security. 
    // Use a disposable variable key, such as "data" here. 
    if (array_key_exists('data', $_POST)) { 
     // Timestamp sent by AJAX 
     if (array_key_exists('ts', $_POST)) { 
      // TODO: verify ts, but beware of time zones! 
      $_SESSION['data'] = $_POST['data']; 
      Header("Content-Type: application/json;charset=UTF-8"); 
      die(json_encode(array('status' => 'OK'))); 
     } 
     die("Error"); 
    } 
    // This is safe (we move unsecurity-ward): 
    $_POST = $_SESSION['data']; 
    unset($_SESSION['data']); // keep things clean. 
?> 

Jetzt ist Ihr Anruf wird

$.post('datadestination.php', { data: list, ts: Date.now() }, function(){ 
    window.open('datadestination.php'); 
}); 

und in Ihrem datadestination.php (oder anderswo) Sie

require 'savepost.php'; 
+0

Kudos!Ich würde es selbst schreiben, aber es ist gut für andere, die auf das gleiche Problem stoßen, um die funktionierende Lösung zu sehen. – zorza

+1

Och, ich hatte es irgendwie vergessen du wolltest es in einem * neuen * Tab öffnen. Fest. – LSerni

Verwandte Themen