将jquery值存储在php变量中

时间:2017-06-19 06:35:56

标签: php ajax

模态按钮包含帖子ID:

<button onclick='getValue("<?=$id?>");' data-toggle="modal" name="submit" data-target="#squarespaceModal">Purchase Now</button>

当我使用alert时,它会正确显示此ID,但我需要将此ID存储在 PHP 变量中

              `function getValue(id){
                       alert(id);
                  // i am trying this but not done
                 // <?php $xyz = ?> id ;

                }`

我也在尝试使用$.post,但在模式中数据不会出现 像

$.post( "<?=$current_url;?>", function( id ) { // $( "#div1" ).html( id ); //alert(id); });

我怎样才能将jquery值存储在php变量中?我见过的大多数例子都是相反的,比如var x =<?php echo $y;?>,但我需要相反。

2 个答案:

答案 0 :(得分:1)

你不能单独使用jQuery;你需要结合使用Ajax和PHP文件。例如: 的 HTML

<button onclick='getValue("<?=$id?>");' data-toggle="modal" name="submit" data-target="#squarespaceModal">Purchase  Now</button>

<强>的jQuery

function getValue(id) {
        // Send Ajax request to ajax_file.php, with id as POST data
        $.post("ajax_file.php", {'id': id}, function(result){ alert(result)});
 }

<强> ajax_file.php

<?php
    session_start();
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
       $_SESSION['id'] = $_POST['id'];
       echo $_SESSION['id'];
     }
?>

答案 1 :(得分:0)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="pricing-table">
    <tr>
      <td id="table-heading"><h1>Leistungen &amp; Preise Telefonservice</h2></td>
      <td>
        <input type="radio" id="test" name="tarif-top">
         <label for="test">AllIn-Order</label>
</td>
      <td class="red-background">AllIn-Time<br>
        <input type="radio" checked name="tarif-top" value="allin-time"/></td>
      <td>AllIn-Contact<br>
        <input type="radio" name="tarif-top" value="allin-contact"/></td>
      <td>
        Enterprise<br>
        <input type="radio" name="tarif-top" value="enterprise"/>
      </td>
    </tr>
    <tr>
      <td>Monatliche Grundgebühr</td>
      <td colspan="3">nur 59,90€</td>
      <td>individuell</td>
    </tr>
    <tr>
      <td>Telefonische Annahmezeit</td>
      <td colspan="3">Mo-Fr 08:00 bis 19:00 Uhr</td>
      <td>24/7</td>
    </tr>
    <tr>
      <td>Kosten pro Minute/Kontakt</td>
      <td>0,69€/Minute</td>
      <td class="red-background">0,89€/Minute</td>
      <td>3,00€/Kontakt</td>
      <td>individuell</td>
    </tr>
    <tr>
      <td>Transaktionsgebühren</td>
      <td>12,5%/Bestellung</td>
      <td class="red-background">—</td>
      <td>—</td>
      <td>individuell</td>
    </tr>
    <tr id="services">
      <td>Enthaltene Leistungen</td>
    </tr>
    <tr>
      <td>Englischsprachiger Telefonservice</td>
      <td colspan="3">Check</td>
      <td>Check</td>
    </tr>
    <tr>
      <td>Kundenservice für Markplätze</td>
      <td colspan="3">Check</td>
      <td>Check</td>
    </tr>
    <tr>
      <td>Bestellannahme für Waren</td>
      <td colspan="3">Check</td>
      <td>Check</td>
    </tr>
    <tr>
      <td>Anrufnotiz via E-Mail</td>
      <td colspan="3">Check</td>
      <td>Check</td>
    </tr>
    <tr>
      <td>Anrufnotiz via E-Mail</td>
      <td colspan="3">Check</td>
      <td>Check</td>
    </tr>
    <tr>
      <td>Monatliches Reporting</td>
      <td colspan="3">Check</td>
      <td>Check</td>
    </tr>
    <tr>
      <td>Weiterleitung Festnetz (DE)</td>
      <td colspan="3">0,09€/Minute</td>
      <td>Check</td>
    </tr>
    <tr>
      <td>Weiterleitung Mobilfunknetz (DE)</td>
      <td colspan="3">0,25€/Minute</td>
      <td>Check</td>
    </tr>
     <tr>
      <td>Buchungsannahme</td>
      <td colspan="3">—</td>
      <td>Check</td>
    </tr>
    <tr>
      <td>Outbound-Kampagnen</td>
      <td colspan="3">—</td>
      <td>Check</td>
    </tr>
    <tr>
      <td></td>
      <td>
        <input type="radio" value="allin-order" name="tarif-bottom"/>
        <br>AllIn-Order
      </td>
      <td class="red-background">
        <input type="radio" checked name="tarif-bottom" value="allin-time"/>
        <br>AllIn-Time
      </td>
      <td>
        <input type="radio" name="tarif-bottom" value="allin-contact"/>
        <br>AllIn-Contact
      </td>
      <td>
        <input type="radio" name="tarif-bottom" value="enterprise"/>
        <br>Enterprise
      </td>
    </tr>
</table>

<!-- END table -->


<h2>Buchungsübersicht</h2>
<div class="row">
  <div class="col-md-6">
    <div class="tarif-choice">
      <p>Ihre Tarifauswahl</p>
      <hr/>
      <div class="allin-time">
    <ul>
      <li>mtl. Grundgebühr 59,90€</li>
      <li>0,89€/Minute</li>
      <li>Servicezeit: Mo-Fr 08:00-19:00 Uhr</li>
        </ul>
        
        <ul class="check">
          <li>Mehrsprachiger Telefonservice</li>
          <li>Bestellannahme für Waren</li>
          <li>Buchungsannahme</li>
          <li>Anrufnotiz via E-Mail</li>
          <li>Monatliches Reporting</li>
          <li>Einzelverbindungsnachweis</li>
        </ul>
      </div>
    </div>
  </div>
  <div class="col-md-6">
    <p>Optionale Leistungen &#124; | Diese Leistungen werden nur abgerechnet,
 soweit Sie sich aktiv für den Service entschieden haben
       <ul class="optional">
         <li><input type="checkbox"/>Service Zeit 24/7
           <p class="dot">Kosten: 19,90 €/E-Mail</p>
        </li>
  <li><input type="checkbox"/>Anrufnotiz via SMS
           <p class="dot">Kosten: 0,20 €/SMS</p>
        </li>
<li><input type="checkbox"/>E-Mail-Service
           <p class="dot">Servicezeit: Mo-Fr 08:00-19:00 Uhr</p>
           <p class="dot">Kosten: 4,49 €/E-Mail</p>
        </li>
<li><input type="checkbox"/>Chat-Service
           <p class="dot"/>Live-Chat. WhatsApp & Facebook-Messenger Service</p>
    <p class="dot"/>Servicezeit: Mo-Fr 08:00-19:00 Uhr</p>
  <p class="dot"/>Kosten (2 Abrechnungsmodelle möglich): </p>
<div class="checkbox-holder">
  <input type="checkbox"/>5,49€/Chat
  <span class="checkbox-space"><input type="checkbox"/>0,69€/Minute</span>
</div>
        </li>
<li><input type="checkbox"/>Click-to-Message Service
           <p class="dot"/>Servicezeit: Mo-Fr 08:00-19:00 Uhr</p>
<p class="dot">0,20 €/SMS</p>
        </li>
      </ul>
  </div>
</div>

这里我已经对php文件进行了ajax调用,在该文件中,我在会话中存储了已发送的id,所以现在你在会话中的php变量中有了这个id