2012-04-01 90 views
1

我需要一些帮助,我想知道如何传递一个值,从PHP变量到jQuery脚本? 我正在做的是打开一个模式窗口,从mysql浏览器创建的元素列表中,所以我需要传递一个变量值的锚点。 这里是我的代码:从PHP传递值到jquery

<?php 
$query_tours = "Select * from tours where feautured = 'Y' " ; 
$result_tours = mysql_query($query_tours); 
while($row=mysql_fetch_array($result_tours)) { 
      $tourID = $row['recid']; 
    $tit_esp = $row['tit_esp']; 
    $adrate = $row['ad_rate']; 
    $chrate = $row['ch_rate']; 
    echo '<div style="border:1px solid #EEE;font-size:12px">'; 
     print "$tit_esp <br>"; 
     print "Adultos : $adrate | Ni&ntilde;os : $chrate "; 
     print "<a href='#?tourid=$tourID' class='myTour_$tourID' >ver  detalles</a>"; 
     print "</div>"; 
    } 

>

这里是jQuery脚本:

<script> 
$(function() { 

    $("#dialog:ui-dialog").dialog("destroy"); 

    var adult = $("#adult"), 
     child = $("#chl"), 
     fecha = $("#datepicker"), 




     allFields = $([]).add(adult).add(child).add(fecha), 
     tips = $(".validateTips"); 

    function updateTips(t) { 
     tips 
      .text(t) 
      .addClass("ui-state-highlight"); 
     setTimeout(function() { 
      tips.removeClass("ui-state-highlight", 1500); 
     }, 500); 
    } 


    function checkLength(o, n, min, max) { 

     if (o.val()> max || o.val()< min) { 
      o.addClass("ui-state-error"); 
      updateTips("El numero de " + n + " debes ser entre " + 
       min + " y " + max + "."); 
      return false; 
     } else { 
      return true; 
     } 
    } 
    function checkFecha() { 
     var dfecha = document.getElementById('datepicker').value; 

     if(dfecha == ""){ 
      //dfecha.addClass("ui-state-error"); 
      updateTips("Fecha Incorrecta"); 
      return false; 
     }else{ 
      return true; 
     } 


    } 

    function checkRegexp(o, regexp, n) { 
     if (!(regexp.test(o.val()))) { 
      o.addClass("ui-state-error"); 
      updateTips(n); 
      return false; 
     } else { 
      return true; 
     } 
    } 

    $("#dialog-form").dialog({ 
     position: 'center', 
     autoOpen: false, 
     draggable: true, 
     height: 300, 
     width: 410, 
     modal: true, 
     show : { 
      transitionIn: 'blind', 
      transitionOut: 'explode', 
     }, 
     open: function(event, ui) { 
      $("#datepicker").datepicker('enable'); 

      alert(regis) 

      $.ajax({ 
       url: 'tour_cont.php', 
       data: "regis=" + regis + "", 
       dataType: 'json', 
       success: function(data){ 
        var titulo = data[1]; 
        var user = data[3];  
        var comenta = data[4]; 
        $("#dialog-form").append("<tr>" + 
         "<td width='21%'>" + titulo + "</td>" + "</tr>"); 

        } 
      }); 

     }, 

     buttons: { 
      "Agregar al Carrito": function() { 
       var bValid = true; 

       bValid = bValid && checkFecha(datepicker); 
       bValid = bValid && checkLength(adult, "adultos", 1, 10); 
       bValid = bValid && checkLength(child, "niños", 0, 10); 

       if (bValid) { 

        $("#users tbody").append("<tr>" + 
         "<td>" + adult.val() + "</td>" + 
         "<td>" + child.val() + "</td>" + 
         "<td>" + fecha.val() + "</td>" + 

        "</tr>"); 
        $(this).dialog("close"); 
       } 
      }, 
      Cancel: function() { 
       $(this).dialog("close"); 
      } 
     }, 
     close: function() { 
      allFields.val("").removeClass("ui-state-error"); 
      $('#datepicker').datepicker('disable'); 
     } 
    }); 

    $('a[class^=myTour]') 
     .button() 
     .click(function() { 
      $("#dialog-form").dialog("open"); 
     }); 




    $("#create-chichen") 
     .button() 
     .click(function() { 
      $("#dialog-form").dialog("open"); 
     }); 


}); 
</script> 

希望有人understad我,谢谢你!先进

+1

你不需要发布所有的代码,只知道是有关您的问题相关的帖子。 – Starx 2012-04-01 04:34:13

回答

3

可以传递PHP的值转换为JavaScript这样

<script type="text/javascript"> 
var myJSval = <?php echo $phpVal; ?>; 
</script>