Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Dwa submit- jeden formularz
Forum PHP.pl > Forum > PHP
kamil881
Witam,
Wiem, że problem przewijał się już na forum, ale mam tutaj konkretną budowę skryptu:
Otóż chcę zrobić, aby w jednym formularzu było zapisywanie i wypisywanie do newslettera naraz.

A to moje wypociny, ale niestety nie działają jak trzeba:
Kod
<form name="txtlist" action="./index.php" target="txtlist" method="get" onsubmit="javascript:window.open('','txtlist','width=300,height=150,toolbar=no,status=no,resizable=no')">
<table border="0" cellpadding="4" cellspacing="0" bgcolor="#efefef" style="border: #dedede 3px double;">
<tr><td>
<input type="hidden" name="type" value="sub"/>
<input type="hidden" name="type" value="unsub"/>
<input type="text" name="email" value="email address" size="20" maxlength="100" onfocus="if (this.value=='email address') this.value=''" onblur="if (this.value=='') this.value='email address'"/>
<input type="submit" name="sub" value="subscribe"/>
<input type="submit" name="unsub" value="unsubscribe"/>
</td></tr>
</table>
EarthCitizen
Może zmieniaj wartość atrybutu action w zależności od akcji:
http://forum.php.pl/index.php?s=&showt...st&p=564293
Kamil Jura
Cytat(kamil881 @ 29.01.2009, 12:47:31 ) *
Witam,
Wiem, że problem przewijał się już na forum, ale mam tutaj konkretną budowę skryptu:
Otóż chcę zrobić, aby w jednym formularzu zrobić zapisywanie i wypisywanie do newslettera naraz.

A to moje wypociny, ale niestety nie działają jak trzeba:
Kod
<form name="txtlist" action="./index.php" target="txtlist" method="get" onsubmit="javascript:window.open('','txtlist','width=300,height=150,toolbar=no,status=no,resizable=no')">
<table border="0" cellpadding="4" cellspacing="0" bgcolor="#efefef" style="border: #dedede 3px double;">
<tr><td>
<input type="hidden" name="type" value="sub"/>
<input type="hidden" name="type" value="unsub"/>
<input type="text" name="email" value="email address" size="20" maxlength="100" onfocus="if (this.value=='email address') this.value=''" onblur="if (this.value=='') this.value='email address'"/>
<input type="submit" name="sub" value="subscribe"/>
<input type="submit" name="unsub" value="unsubscribe"/>
</td></tr>
</table>


Wcale się im nie dziwię smile.gif Przy okazji - osobiście lubię metodę post i na niej radziłbym Ci się skupić.

Usuń te 2 linijki
Kod
<input type="hidden" name="type" value="sub"/>
<input type="hidden" name="type" value="unsub"/>


Skupmy się na 2 submit'ach.

Wciśnięty submit również przekazuje wartość [przez POST], tak więc pliku index.php kiedy odbierasz dane zrób coś takiego:

  1. <?php
  2. if(isset($_POST['type'] && $_POST['type'] == 'sub') {
  3.  
  4. // tutaj sobie dodasz adres mailowy
  5.  
  6. }elseif(isset($_POST['type']) && $_POST['type'] == 'unsub') {
  7.  
  8. // tutaj usuniesz
  9.  
  10. }
  11. ?>
kamil881
To jest plik index.php

Dodać to tu gdzieś po prostu?
Kod
<?

require('config.inc.php');

require('functions.inc.php');



// Max execution time of script. This does not include sleep time, so only restricts time

// allowed for mailing.

set_time_limit(3600);



$type = $_GET['type'];

$addr = stripslashes(urldecode($_GET['email']));



if ($type == 'sub') {

  // subscribe address. Actually, all it does is send the confirmation email

  if (!preg_match('#^([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)?[\w]+[^\".,?! ])$#i', $addr)) {

    $err = 'Your email address appears to be invalid.';

  }

  if (!isset($err)) {

    $f = openfile($cfg['listfile']);

    $exists = false;

    while ($item = readitem($f)) {

      if ($item['addr'] == $addr) {$exists = true;break;}

    }

    if ($exists) {

      $err = 'That address is already subscribed to the list.';

    }

  }

  if (!isset($err)) {

    // send confirmation email

    $headers = 'From: '.$cfg['from'];

    if ($cfg['headers']) {

      foreach ($cfg['headers'] as $val) $headers .= "\n".$val;

    }

    $headers .= "\n".$cfg['header_plain'];

    if (!@mail($addr, $cfg['subj_conf'], sprintf($cfg['msg_conf'], '?type=confirm&email='.urlencode($addr)), $headers)) {

      $err = 'There was an error sending the confirmation email. Please try again later.';

    }

  }

  if (!isset($err)) {

    // confirmation sent

    if (isset($cfg['returnto_conf']) && $cfg['returnto_conf'] != '') {

      header('Location: '.$cfg['returnto_conf']);

    }

    else {

      printf($cfg['template'], 'Awaiting confirmation', 'An email has been sent to your address, '.$addr.', with a URL you must visit to confirm your subscription.');

    }

  }

  else {

    // problem with subscription

    echoerr($err);

  }

}

elseif ($type == 'unsub') {

  // unsubscribe address

  $f = openfile($cfg['listfile']);

  $exists = false;

  while ($item = readitem($f)) {

    if ($item['addr'] == $addr) {$exists = true;break;}

  }

  if ($exists) {

    delitem($f, $item['id']);

  }

  else {

    $err = 'That address was not found.';

  }

  if (!isset($err)) {

    // unsubscribe successful

    if (isset($cfg['returnto_unsub']) && $cfg['returnto_unsub'] != '') {

      header('Location: '.$cfg['returnto_unsub']);

    }

    else {

      printf($cfg['template'], 'Unsubscribed', 'Your address, '.$addr.' has successfully been unsubscribed from our mailing list.');

    }

  }

  else {

    // problem with unsubscription

    echoerr($err);

  }

}

elseif ($type == 'confirm') {

  // subscribe a confirmed address

  if (!preg_match('#^([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)?[\w]+[^\".,?! ])$#i', $addr)) {

    $err = 'Your email address appears to be invalid. If you are pasting the URL from your confirmation email, make sure the URL wasn\'t split into two lines.';

  }

  if (!isset($err)) {

    $f = openfile($cfg['listfile']);

    $exists = false;

    while ($item = readitem($f)) {

      if ($item['addr'] == $addr) {$exists = true;break;}

    }

    if ($exists) {

      $err = 'That address is already subscribed to the list.';

    }

  }

  if (!isset($err)) {

    // subscribe successful

    writeitem($f, $addr);

    if (isset($cfg['returnto_sub']) && $cfg['returnto_sub'] != '') {

      header('Location: '.$cfg['returnto_sub']);

    }

    else {

      printf($cfg['template'], 'Subscribed', 'Your address, '.$addr.', is now subscribed to our mailing list!');

    }

  }

  else {

    // problem with subscription

    echoerr($err);

  }

}



function echoerr($err) {

  printf($GLOBALS['cfg']['template'], 'Error', '<font color="red">Error: '.$err.'</font>');

}

?>
Kamil Jura
  1. <?php
  2. if ($type == 'sub') {
  3.  
  4.  // subscribe address. Actually, all it does is send the confirmation email
  5.  
  6.  if (!preg_match('#^([a-z0-9-_.]+?)@([w-]+.([w-.]+.)?[w]+[^\".,?! ])$#i', $addr)) {
  7.  
  8.    $err = 'Your email address appears to be invalid.';
  9.  
  10.  }
  11.  
  12.  if (!isset($err)) {
  13.  
  14.    $f = openfile($cfg['listfile']);
  15.  
  16.    $exists = false;
  17.  
  18.    while ($item = readitem($f)) {
  19.  
  20.      if ($item['addr'] == $addr) {$exists = true;break;}
  21.  
  22.    }
  23.  
  24.    if ($exists) {
  25.  
  26.      $err = 'That address is already subscribed to the list.';
  27.  
  28.    }
  29.  
  30.  }
  31. ?>


Tutaj to wywal i napisz to co Ci podałem. Możesz w to wtoczyć jeszcze tego preg_match i tyle.
A zmienne superglobalne z $_GET zamień na $_POST.

Kombinuj. Powinno działać

* Żeby nie pisać nowego posta:

Zrobiłem edit w powyższym temacie. Tamten kod powinien działać:)

Pozdrawiam
kamil881
Wywala mi błąd w tej linijce:
Kod
if(isset($_POST['type'] && $_POST['type'] == 'sub') {
nospor
1) Jak ci wywala blad, to nalezy go podac!!!
2)
  1. <?php
  2. if(isset($_POST['type']) && $_POST['type'] == 'sub')
  3. ?>

3) Stosuj wlasciwe bbcode czyli [php] dla kodu php a nie [code]
kamil881
Ok, dzięki. Myślę, że teraz już sobie poradzę.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.