<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>株式会社アルバステラ &#187; php</title>
	<atom:link href="http://albastella.co.jp/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://albastella.co.jp</link>
	<description>WEBサイト製作・販売・サポート</description>
	<lastBuildDate>Wed, 03 Oct 2018 07:11:11 +0000</lastBuildDate>
	<language>ja</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.8.41</generator>
	<item>
		<title>javascriptからphpへフォームデータを渡す</title>
		<link>http://albastella.co.jp/2015/12/14/javascript%e3%81%8b%e3%82%89php%e3%81%b8%e3%83%95%e3%82%a9%e3%83%bc%e3%83%a0%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92%e6%b8%a1%e3%81%99/</link>
		<comments>http://albastella.co.jp/2015/12/14/javascript%e3%81%8b%e3%82%89php%e3%81%b8%e3%83%95%e3%82%a9%e3%83%bc%e3%83%a0%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92%e6%b8%a1%e3%81%99/#comments</comments>
		<pubDate>Mon, 14 Dec 2015 07:06:37 +0000</pubDate>
		<dc:creator><![CDATA[albastella]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://albastella.co.jp/?p=203</guid>
		<description><![CDATA[ページ遷移なしのメールフォームを設置する案件で、 javascriptからphpへフォームデータを渡す必要があり、そのときの覚書。 結局これは使わなかったのやけど。。 html php ざっくり言うと、javascriptからpost値をphpに渡して、エラーがなければ送信して、結果をjavascriptに返すってことをしています。 phpのエラーチェックとかは適当に書いてますが、send.phpに直接アクセスされて空メールが送信されるのを防止する意味合いぐらいで設定してます。 今回は未完成のままになってしまいますが。 javascriptでフォームの未入力などをチェック。→入力に誤りがない状態でphpに値を送信。→送信完了後フォームをリセットというのが最終予定でした。]]></description>
				<content:encoded><![CDATA[<p>ページ遷移なしのメールフォームを設置する案件で、<br />
javascriptからphpへフォームデータを渡す必要があり、そのときの覚書。<br />
結局これは使わなかったのやけど。。</p>
<h4>html</h4>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;ja&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;title&gt;example&lt;/title&gt;
    
    &lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;script&gt;
    $(document).ready(function()
    {
		
        $('#send').click(function()
        {
            //postメソッドで送るデータを定義 var data = {パラメータ名 : 値};
           var data = {
		namae : $('#namae').val(),
		email : $('#email').val(),
		ask : $('#ask').val()
	};

            $.ajax({
                type: &quot;post&quot;,
                url: &quot;send.php&quot;,
                data: data,
                //Ajax通信が成功した場合
                success: function(data, dataType)
                {
                //PHPから返ってきたデータの表示
                alert(data);
			    //送信完了後フォームの内容をリセット
				document.forms[0].elements[0].value=&quot;&quot;;
				document.forms[0].elements[1].value=&quot;&quot;;
			    document.forms[0].elements[2].value=&quot;&quot;;
                },
               //Ajax通信が失敗した場合のメッセージ
                error: function()
                {
                alert('メールの送信が失敗しました。');
                }
            });
            return false;
        });
    });
    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id=&quot;contactForm&quot; name=&quot;contactForm&quot; method=&quot;post&quot; &gt;
&lt;ul&gt;
&lt;li&gt;&lt;input type=&quot;text&quot; id=&quot;namae&quot; name=&quot;namae&quot; placeholder=&quot;名前&quot;&gt;&lt;/li&gt;
&lt;li&gt;&lt;input type=&quot;email&quot; id=&quot;email&quot; name=&quot;email&quot; placeholder=&quot;E-mail&quot; &gt;&lt;/li&gt;
&lt;li&gt;&lt;textarea name=&quot;ask&quot; id=&quot;ask&quot; placeholder=&quot;お問い合わせ内容&quot; &gt;&lt;/textarea&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p class=&quot;more&quot;&gt;&lt;input id=&quot;send&quot; type=&quot;submit&quot; value=&quot;送る&quot;&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<h4>php</h4>
<pre class="brush: php; title: ; notranslate">
&lt;?php
header(&quot;Content-type: text/plain; charset=UTF-8&quot;);
$sender_name = $_POST['namae'];
$sender_mail_address = $_POST['email'];
$contact_body = $_POST['ask'];

//空白チェック
    $errorflag = 0;
	if ($sender_name == null) {
		$error_name = &quot;お名前が未入力です&quot;;
		$errorflag = 1;
	}else{$error_name = &quot;&quot;;}
	if ($sender_mail_address == null) {
		$error_mail = &quot;メールアドレスが未入力です&quot;;
		$errorflag = 1;
	}else{$error_mail = &quot;&quot;;}
	if ($contact_body == null) {
		$error_ask = &quot;お問い合わせ内容が未入力です&quot;;
		$errorflag = 1;
	}else{$error_ask = &quot;&quot;;}
	
if ($errorflag == 1)
{
    if($error_name != &quot;&quot;){ echo $error_name.&quot;\n&quot;;}
	if($error_mail != &quot;&quot;){ echo $error_mail.&quot;\n&quot;;}
	if($error_ask != &quot;&quot;){ echo $error_ask.&quot;\n&quot;;}
    
}
else
{
   //ここにメール送信の設定
   
   echo &quot;送信が完了しました&quot;;
}
?&gt;
</pre>
<p>ざっくり言うと、javascriptからpost値をphpに渡して、エラーがなければ送信して、結果をjavascriptに返すってことをしています。</p>
<p>phpのエラーチェックとかは適当に書いてますが、send.phpに直接アクセスされて空メールが送信されるのを防止する意味合いぐらいで設定してます。<br />
今回は未完成のままになってしまいますが。<br />
javascriptでフォームの未入力などをチェック。→入力に誤りがない状態でphpに値を送信。→送信完了後フォームをリセットというのが最終予定でした。</p>
]]></content:encoded>
			<wfw:commentRss>http://albastella.co.jp/2015/12/14/javascript%e3%81%8b%e3%82%89php%e3%81%b8%e3%83%95%e3%82%a9%e3%83%bc%e3%83%a0%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92%e6%b8%a1%e3%81%99/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
