0

メールリストの登録フォームを作成しています。フォームを送信した後、すべてのデータをデータベースに登録してから、そのデータを別のページに「再送信」して、ユーザーをメールリストに登録したいと考えています。

データベースにすべてのデータを登録する方法は知っていますが、データを別のページに「再送信」する方法がわかりません。

なぜそれが必要なのですか?私はメールリストシステムとして GNU Mailman を使っているので必要です。このソフトウェアは、名前、電子メール、パスワードのみを保存するため、「国」、「都市」、「性別」などのカスタム フィールドを含む登録フォームを作成できません。

4

2 に答える 2

4

CURLを使用して HTTP リクエストを作成し、それを 2 番目のスクリプトに送信するPHP スクリプトを作成できます。

    /*
      get your data from db and put it into an associated array:

       $post_array['name] = $row['name'];
       $post_array['country] = $row['country'];

       ..and so on

    */

   //serialize the post array into a string 
   foreach($post_array as $key=>$value) { $fields .= $key.'='.$value.'&'; }        
   rtrim($fields,'&');

    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL, 'http://domain.com/script.php');
    curl_setopt($ch,CURLOPT_POST,count($post_array);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);


    $response= curl_exec($ch); //submit it..
    $httpCode= curl_getinfo($ch, CURLINFO_HTTP_CODE); //use $httpCode to figure out if it was successful (200) or not.
于 2011-05-23T05:17:04.213 に答える
0

私の簡単な試み。フォームが送信される最初のページ

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="" method="post">
<input type="text"  name="email"/>
<input type="text"  name="name"/>
<input type="submit" value="registration" name="register" />
</form>

<?php 

//connection with database.
$conn=mysql_connect("localhost","root","");

mysql_select_db("test");

//check if form submitted
  if(isset($_POST['register']))
  {
  $result=mysql_query("INSERT INTO testdata(name,email)   values('".$_POST['name']."','".$_POST['email']."')") or die(mysql_error()); 

 if(mysql_affected_rows($conn)>0)
 {
  echo "Done";

 $url = 'http://localhost/TESTCODE/get-post.php';

 $fields = array(
        'name'=>urlencode($_POST['name']),
        'email'=>urlencode($_POST['email'])           
       );


   /* on my install of PHP 5.3, http_build_query() seems to use &amp; as the default  separator.  */



   $fields_string=http_build_query($fields);



  //open connection
  $ch = curl_init();


  //set the url, number of POST vars, POST data
  curl_setopt($ch,CURLOPT_URL,$url);

 curl_setopt($ch,CURLOPT_POST,count($fields));

  curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

 //execute post

 $result = curl_exec($ch);

 //close connection

 curl_close($ch);

}
 }
 ?>

 </body>
 </html>

2ページ目のget-post.phpで、curlを介してフォームを送信しました

 <?php
 $conn=mysql_connect("localhost","root","");
mysql_select_db("test");
$result=mysql_query("INSERT INTO testdata(name,email)     values('".$_POST['name']."','".$_POST['email']."')") or die(mysql_error()); 
  ?>

簡単なチェックのためにやっただけです。必要に応じて変更する必要があります。ここでは、2つの異なるページにデータを2回挿入しました

于 2011-05-23T07:33:39.493 に答える