htmlとcssとphp-初学非同期処理とおまけ-No.3
2022.11.14
おはようございます。月曜日の朝ですね‥お仕事探しは続いています。
さて、非同期処理とは何かと問われると詰まりますが・・・。いつ結果を返してくれない処理といえば良いのでしょうか。じゃ同期処理はといえば仕事が終わるまで次の仕事を進めない事といえば良いのかな🤔、教えるのが下手なのでぐぐってみてください。
document.querySelector(".btn").addEventListener("click", () => {
let p = [document.querySelector("[name='name']").value, document.querySelector("[name='text']").value];
[...document.querySelectorAll(".put")].forEach((elm, index) => {
elm.innerText = p[index];
});
document.querySelector("#box2").style.display = "none";
document.querySelector("#data").insertAdjacentHTML("beforeend", `<button class="btn2" type="button">非同期送信</button>`);
document.querySelector(".btn2").addEventListener("click", async () => {
//submit code
let url = "./submit.php";
let data = (() => {
return ([...document.querySelectorAll(".put")].map((elm, index) => {
return "test" + index + "=" + elm.innerText;
})).join("&");
})();
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
"Content-Length": data.length,
'Accept': 'application/json'
},
body: data //JSON.stringify(data)
};
await fetch(url, options).then(response =>
response.json()
).then(resultdata => {
document.getElementById("view").insertAdjacentHTML("beforeend", resultdata.test0 + "," + resultdata.test1);
}).catch(error => {
console.log(error);
});
});
});
function imgchg(imagename){
document.body.setAttribute("style","background-image: url(./assets/images/" + imagename + ".jpg");
}
ぐぐると自分言っていた意味がなんとなく分かるかと思います。ちなみに非同期動画(youtube)のおまけとして、背景画像を変える処理のソースコードを書いています。プログラムコードとしては3行ですが、初学者の方はこの3行のソースコードの方が面白いのかもしれません。自分が書いたソースコードを所々、変更して動かしてみてください。そうすることで、徐々にコードの意味が理解してくると思います。
追伸:サンプルサイトの動画は少しお休みします()?
https://358tool.com/sample-site/
タグ
3, 39, addEventListener, btn, click, css, document, gt, html, let, name, no, php, querySelector, querySelectorAl, quot, Text, value, いつ, おまけ, お仕事, 事, 仕事, 何, 処理, 初学, 同期, 月曜日, 朝, 次, 結果,
April Fools' Day :)
2020.04.01
Today is April Fool’s Day, isn’t it? Let’s all be careful not to be deceived by lies and hoaxes. Incidentally, I think Google will also release their annual lie app.
The video is not particularly meaningful.It’s just that it was the name of a channel on YOUTUBE that has the same name as where I live.
Click here to register your channel on YOUTUBE.
https://www.youtube.com/channel/UCN2C94LXAg1tXUVdyfz3Itw
※Postscript
Google cancels its infamous April Fools’ jokes this year
https://www.theverge.com/2020/3/27/21197260/google-cancels-april-fools-jokes-2020-coronavirus-covid19-pranks
タグ
:, 1, 2, 3, all, also, and, annual, App, April, as, be, BY, careful, channel-, click, com, day, deceived, Fools', Google, has, here, hoaxes, https, Incidentally, is, isn't, IT, It's, Itw, just, Let's, lie, lies, live, LXAg, meaningful, name, not, of, ON, particularly, register, release, same, that, The, their, think, To, Today, tXUVdyfz, UCN, video, was, where, will, www, Your, youtube,
Javascript? EVENT処理の際に引数を渡す。
2019.04.26
Javascript? EVENT処理の際に引数を渡す方法。
ちなみにもっと簡略化できます。
//NG
$(function(){
let hoge = "test";
$("input[type='text']").on("change",(function(hoge){
console.log(hoge);
})(hoge));
});
//OK!
//js
function hoge(a){
return function(){
console.log(a + this.value);
};
};
document.querySelector('input[type="text"]').addEventListener("click",hoge("aaaaaaa~"),false);
//jq
$(function(){
let hoge = function(a){
return function(){
console.log(a + $(this).val());
};
};
$("input[type='text']").on("change",hoge("test="));
});
タグ
aaaaaaa, addEventListener, change, click, console, document, EVENT, false, fu, function, hoge, input, javascript, jq, JS, let, log, NG, OK, ON, querySelector, return, test, Text, this, type, value, 処理, 引数, 方法, 簡略, 際,
WordPressを実践勉強中、ajaxについてうにゃ。
2017.10.27
WordPressでajaxを動かそうするとき、WordPressのルールに
則って動かさないと動かないらしい。
なので、このような記述を書かないといけない。
まず、非同期処理の送信先に、admin-ajax.phpというファイルを指定。
そこへPOSTかGETしないといけない。そしてデータとして
アクションさせるファンクション名を記述して
上げないといけないらしい。
こんな感じになる。
$( '#submit' ).on( 'click', function(){ $.ajax({ type: 'POST', url: <?=admin_url('admin-ajax.php')?>, data: { 'action' : 'hoge_f', }, dataType: "json", success: function( obj){ alert( obj ); } }); return false; });
次にWPテーマのファンクションファイルに下記を追加記述。
function hoge_f(){ echo json_encode($_POST); die(); } add_action( 'wp_ajax_hoge_f', 'hoge_f' ); add_action( 'wp_ajax_nopriv_hoge_f', 'hoge_f' );
ここでミソなのが、die();という部分、これを除けると0という文字も出力されてしまう。
ちなみにwp_ajaxがログイン時の振る舞い、wp_ajax_noprivがログアウト(=訪問者)時の
振る舞いだそうだ。
この2つの勘所だけ覚えとけばWordPressで非同期処理が
行えるのではないだろうか、もっと詳しく知りたい場合、admin-ajax.phpの中身を
見たらよいのだろうけど、にわかWordPress職人さんはコレだけ
覚えとけば良さそうだと。
タグ
'hoge_f', 'wp_ajax_nopriv_hoge, Action, admin-ajax.php, AM, click, dataType, echo json_encode, function hoge, function hoge_f, obj, POST, return false, submit, WordPress職人, ファンクション, ファンクションファイル, ログアウト, 体, 非同期処理,