ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น

ThaiSEOBoard.comพัฒนาเว็บไซต์Programmingใครใช้ sammy JS เข้ามาหน่อยครับ ส่งค่า POST ยังไงครับ
หน้า: [1]   ลงล่าง
พิมพ์
ผู้เขียน หัวข้อ: ใครใช้ sammy JS เข้ามาหน่อยครับ ส่งค่า POST ยังไงครับ  (อ่าน 881 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
tae861
Verified Seller
ก๊วนเสียว
*

พลังน้ำใจ: 29
ออฟไลน์ ออฟไลน์

กระทู้: 477



ดูรายละเอียด
« เมื่อ: 15 พฤษภาคม 2017, 21:38:55 »

พอดีจะส่งค่า  POST ไปที่ไฟล์ search
แล้วอยากให้มัน redirect ไปที่หน้า #/search/{ค่าที่ได้รับมา คือ $_POST[name]
แนะนำหน่อยครับ ผมไม่รู้จะทำยังไง
บันทึกการเข้า
maneemeena
คนรักเสียว
*

พลังน้ำใจ: 8
ออฟไลน์ ออฟไลน์

กระทู้: 189



ดูรายละเอียด
« ตอบ #1 เมื่อ: 16 พฤษภาคม 2017, 10:10:40 »

ลองอ่าน Doc ดูครับ เรื่อง route
http://sammyjs.org/docs/routes

ส่วนตัวไม่เคยใช้ครับ เพิ่งเคยได้ยินเลย เข้าไปดู fork ใน github Latest commit 245dd7f  on Sep 3, 2014 ถือว่านานเลยนะครับ
บันทึกการเข้า
maneemeena
คนรักเสียว
*

พลังน้ำใจ: 8
ออฟไลน์ ออฟไลน์

กระทู้: 189



ดูรายละเอียด
« ตอบ #2 เมื่อ: 16 พฤษภาคม 2017, 10:17:28 »

ไหนๆก็ลองแล้ว ทดสอบดูละกันครับ ไม่ยาก
โค๊ด:
        this.post('#/post/url', function() {
          this.redirect('#/search/' + this.params['name']);
        });

        this.get('#/search/:name', function() {
         alert(this.params['name']);
        });


บันทึกการเข้า
tae861
Verified Seller
ก๊วนเสียว
*

พลังน้ำใจ: 29
ออฟไลน์ ออฟไลน์

กระทู้: 477



ดูรายละเอียด
« ตอบ #3 เมื่อ: 16 พฤษภาคม 2017, 12:36:16 »

ไหนๆก็ลองแล้ว ทดสอบดูละกันครับ ไม่ยาก
โค๊ด:
        this.post('#/post/url', function() {
          this.redirect('#/search/' + this.params['name']);
        });

        this.get('#/search/:name', function() {
         alert(this.params['name']);
        });

แบบนี้ลองแล้วครับ มันไม่ยอม redirect ให้ เหมือน this.post ไม่ทำงานเลยครับ


บันทึกการเข้า
maneemeena
คนรักเสียว
*

พลังน้ำใจ: 8
ออฟไลน์ ออฟไลน์

กระทู้: 189



ดูรายละเอียด
« ตอบ #4 เมื่อ: 16 พฤษภาคม 2017, 13:19:29 »

กดดู log ครับ เออเรอว่าไงบ้าง
บันทึกการเข้า
tae861
Verified Seller
ก๊วนเสียว
*

พลังน้ำใจ: 29
ออฟไลน์ ออฟไลน์

กระทู้: 477



ดูรายละเอียด
« ตอบ #5 เมื่อ: 16 พฤษภาคม 2017, 13:51:22 »

กดดู log ครับ เออเรอว่าไงบ้าง

กดดุยังไงครับผมโง่ Js มากๆๆ  wanwan014
บันทึกการเข้า
maneemeena
คนรักเสียว
*

พลังน้ำใจ: 8
ออฟไลน์ ออฟไลน์

กระทู้: 189



ดูรายละเอียด
« ตอบ #6 เมื่อ: 16 พฤษภาคม 2017, 14:53:33 »

กดดู log ครับ เออเรอว่าไงบ้าง

กดดุยังไงครับผมโง่ Js มากๆๆ  wanwan014
ในโครม คลิกขวา เลือก inspect เลือกแท็บ console ครับ มือใหม่ js ทำไมถึงเลือกใช้ framework เลยละครับ น่าจะลองหาพื้นฐานก่อนนะครับ
บันทึกการเข้า
tae861
Verified Seller
ก๊วนเสียว
*

พลังน้ำใจ: 29
ออฟไลน์ ออฟไลน์

กระทู้: 477



ดูรายละเอียด
« ตอบ #7 เมื่อ: 16 พฤษภาคม 2017, 15:01:45 »

กดดู log ครับ เออเรอว่าไงบ้าง

กดดุยังไงครับผมโง่ Js มากๆๆ  wanwan014
ในโครม คลิกขวา เลือก inspect เลือกแท็บ console ครับ มือใหม่ js ทำไมถึงเลือกใช้ framework เลยละครับ น่าจะลองหาพื้นฐานก่อนนะครับ


ผมให้ sammy จัดการพวก url กับ get ไฟล์แบบไม่เปลี่ยนหน้าครับ
ผมถนัด php ซ่ะส่วนใหญ่ครับ
บันทึกการเข้า
tae861
Verified Seller
ก๊วนเสียว
*

พลังน้ำใจ: 29
ออฟไลน์ ออฟไลน์

กระทู้: 477



ดูรายละเอียด
« ตอบ #8 เมื่อ: 16 พฤษภาคม 2017, 15:06:30 »

code ครับ

index.html
โค๊ด:
<form action="#/search/" method="post" target="_self" accept-charset="UTF-8">Search : <input name="name" type="text" id="name" size="40"><input type="submit"></form>

app.js
โค๊ด:
(function($) {

  var app = $.sammy('#app', function() {
    this.use('Template');



    this.get('#/', function(context) {
      context.app.swap('');
      $.each(this.items, function(i, item) {
        context.render('templates/main.php', {id: i, item: item})
               .appendTo(context.$element());
      });
    });
   

 
  });

  $(function() {
    app.run('#/index/');
  });

})(jQuery);
ครับ
บันทึกการเข้า
maneemeena
คนรักเสียว
*

พลังน้ำใจ: 8
ออฟไลน์ ออฟไลน์

กระทู้: 189



ดูรายละเอียด
« ตอบ #9 เมื่อ: 16 พฤษภาคม 2017, 16:08:18 »

code ครับ

index.html
โค๊ด:
<form action="#/search/" method="post" target="_self" accept-charset="UTF-8">Search : <input name="name" type="text" id="name" size="40"><input type="submit"></form>

app.js
โค๊ด:
(function($) {

  var app = $.sammy('#app', function() {
    this.use('Template');



    this.get('#/', function(context) {
      context.app.swap('');
      $.each(this.items, function(i, item) {
        context.render('templates/main.php', {id: i, item: item})
               .appendTo(context.$element());
      });
    });
   

 
  });

  $(function() {
    app.run('#/index/');
  });

})(jQuery);
ครับ

งงเลยครับ เห็นตอนแรกถามเรื่องค้นหานี่ครับ แต่โค้ดที่โพสมันไม่เกี่ยวกันเลย อันนี้ผมทดสอบในเครื่องผม
โค๊ด:
  <script type="text/javascript" charset="utf-8">
    ;(function($) {
      var app = $.sammy('#main', function() {
        this.debug = true;
        var form_fields = null;

        this.get('#/', function() {
           this.redirect('#/form');
        });

        this.get('#/search/:name', function() {
         alert(this.params['name']);
        });

        this.get('#/form', function() {
          this.partial('files/form_search.html');
        });

        this.post('#/search/query', function(context) {
  $.ajax({
          url: 'post/post.php',
  type: 'post',
  data: 'name='   this.params['name'],
          dataType: 'json',
          success: function(data) {

  if (data['name']) {
      context.redirect('#/search/'   data['name']);
  } else {
  alert("Not found!!!");
  }
           
          }

        });
         
        });

      });

      $(function() {
        app.run('#/');
      });

    })(jQuery);
  </script>


อันนี้โค้ด form_search.html
โค๊ด:
<form action="#/search/query" method="post">
  <p><label for="name">Search:</label><input type="text" name="name" /></p>
  <p><input type="submit" value="Search" /></p>
</form>

อันนี้โค้ด post/post.php

โค๊ด:
<?php
$json 
= [];

if (isset(
$_POST['name'])) {
$json['name'] = $_POST['name'];
}

echo 
json_encode($json);

ทั้งหมดทำงานปกติครับ
บันทึกการเข้า
tae861
Verified Seller
ก๊วนเสียว
*

พลังน้ำใจ: 29
ออฟไลน์ ออฟไลน์

กระทู้: 477



ดูรายละเอียด
« ตอบ #10 เมื่อ: 16 พฤษภาคม 2017, 17:20:08 »

code ครับ

index.html
โค๊ด:
<form action="#/search/" method="post" target="_self" accept-charset="UTF-8">Search : <input name="name" type="text" id="name" size="40"><input type="submit"></form>

app.js
โค๊ด:
(function($) {

  var app = $.sammy('#app', function() {
    this.use('Template');



    this.get('#/', function(context) {
      context.app.swap('');
      $.each(this.items, function(i, item) {
        context.render('templates/main.php', {id: i, item: item})
               .appendTo(context.$element());
      });
    });
   

 
  });

  $(function() {
    app.run('#/index/');
  });

})(jQuery);
ครับ

งงเลยครับ เห็นตอนแรกถามเรื่องค้นหานี่ครับ แต่โค้ดที่โพสมันไม่เกี่ยวกันเลย อันนี้ผมทดสอบในเครื่องผม
โค๊ด:
  <script type="text/javascript" charset="utf-8">
    ;(function($) {
      var app = $.sammy('#main', function() {
        this.debug = true;
        var form_fields = null;

        this.get('#/', function() {
           this.redirect('#/form');
        });

        this.get('#/search/:name', function() {
         alert(this.params['name']);
        });

        this.get('#/form', function() {
          this.partial('files/form_search.html');
        });

        this.post('#/search/query', function(context) {
  $.ajax({
          url: 'post/post.php',
  type: 'post',
  data: 'name='   this.params['name'],
          dataType: 'json',
          success: function(data) {

  if (data['name']) {
      context.redirect('#/search/'   data['name']);
  } else {
  alert("Not found!!!");
  }
           
          }

        });
         
        });

      });

      $(function() {
        app.run('#/');
      });

    })(jQuery);
  </script>


อันนี้โค้ด form_search.html
โค๊ด:
<form action="#/search/query" method="post">
  <p><label for="name">Search:</label><input type="text" name="name" /></p>
  <p><input type="submit" value="Search" /></p>
</form>

อันนี้โค้ด post/post.php

โค๊ด:
<?php
$json 
= [];

if (isset(
$_POST['name'])) {
$json['name'] = $_POST['name'];
}

echo 
json_encode($json);

ทั้งหมดทำงานปกติครับ



ทำได้แล้วครับ ผมต้องสร้าง form ไว้อีกไฟล์แล้วใช้ sammy เรียกใช้
ผมไม่สามารถสร้าง form ไว้บน index ได้เลย เหมือนมัน Error เส้นผมบังภูเขามานานเลยครับ
บันทึกการเข้า
หน้า: [1]   ขึ้นบน
พิมพ์