需要添加一个输出的图像,一个隐藏的id,和checkout按钮到这个php/ajax脚本和我卡住


need to add an output for an image, a hidden id, and a checkout button to this php/ajax script and am stuck

我是php和ajax的新手。我有一个适合我的剧本,它几乎做了我想做的事情。但是我希望添加这段代码在有图像显示,一旦一个产品被完全选中,它有id。

$dynamic_image .= '
        <a href="product.php?d_id=' . $did . '"><img style="height:auto; width:auto; max-width:7em; max-height:10em; padding :12px; margin: 9px; border: 3px solid #e3e3e3;" src="design_images/' . $did . '.png" alt="' . $name . '"   /></a>';

我还想隐藏'id',或者至少不是一个选择选项。并且一旦产品被完全选中,价格和添加到购物车按钮就会显示。

我知道这要求很多,但任何帮助都会很棒,因为我现在完全被困住了。谢谢你!

下面是select_list.php文件

<?php
// Multiple select lists - http://coursesweb.net/ajax/
if(!isset($_SESSION)) session_start();
// Here add your own data for connecting to MySQL database
$server = 'localhost';
$user = 'root';
$pass = '';
$dbase = 'mystore';
// Here add the name of the table and columns that will be used for select     lists, in their order
// Add null for 'col_description' if you don`t want to display their data too
$table = 'products';
$ar_cols = array('category', 'subcategory', 'size', 'color', 'id', 'price');
$preid = 'slo_';        // a prefix used for element's ID, in which Ajax     will add <select>
$col = $ar_cols[0];     // the variable used for the column that wil be selected
$re_html = '';          // will store the returned html code
$pre_html = '';
// if there is data sent via POST, with index 'col' and 'wval'
if(isset($_POST['col']) && isset($_POST['wval'])) {
 // set the $col that will be selected and the value for WHERE (delete tags and external spaces in $_POST)
  $col = trim(strip_tags($_POST['col']));
  $wval = "'".trim(strip_tags($_POST['wval']))."'";}
$key = array_search($col, $ar_cols);            // get the key associated with the value of $col in $ar_cols
$wcol = $key===0 ? $col : $ar_cols[$key-1];     // gets the column for the WHERE clause
$_SESSION['ar_cols'][$wcol] = isset($wval) ? $wval : $wcol;    // store in SESSION the column and its value for WHERE
// gets the next element in $ar_cols (needed in the onchange() function in <select> tag)
$last_key = count($ar_cols)-1;
$next_col = $key<$last_key ? $ar_cols[$key+1] : '';
$conn = new mysqli($server, $user, $pass, $dbase);     // connect to the MySQL database
if (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); }     // check connection
// sets an array with data of the WHERE condition (column=value) for SELECT query
for($i=1; $i<=$key; $i++) {
  $ar_where[] = '`'.$ar_cols[$i-1].'`='.$_SESSION['ar_cols'][$ar_cols[$i-1]];}
// define a string with the WHERE condition, and then the SELECT query
$where = isset($ar_where) ? ' WHERE '. implode($ar_where, ' AND ') : '';
$sql = "SELECT DISTINCT `$col` FROM `$table`".$where;
$result = $conn->query($sql);       // perform the query and store the result
// if the $result contains at least one row
if ($result->num_rows > 0) {
  // sets the "onchange" event, which is added in <select> tag
  $onchg = $next_col!==null ? " onchange='"ajaxReq('$next_col', this.value);'"" : '';
  // sets the select tag list (and the first <option>), if it's not the last column
  if($col!=$ar_cols[$last_key]) $re_html = $col. ': <select name="'. $col. '"'. $onchg. '><option>- - -</option>';
  while($row = $result->fetch_assoc()) {
    // if its the last column, reurns its data, else, adds data in OPTION tags
    if($col==$ar_cols[$last_key]) $re_html .= '<br/>$'. $row[$col];
    elseif($col==$ar_cols[$last_key]) $pre_html .= '<br/>$'. $row[$col];
    else $re_html .= '<option value="'. $row[$col]. '">'. $row[$col].    '</option>';  }
  if($col!=$ar_cols[$last_key]) $re_html .= '</select> ';        // ends the Select list}    else { $re_html = '0 results';}
$conn->close();
// if the selected column, $col, is the first column in $ar_cols
if($col==$ar_cols[0]) {
  // adds html code with SPAN (or DIV for last item) where Ajax will add the select dropdown lists
  // with ID in each SPAN, according to the columns added in $ar_cols
  for($i=1; $i<count($ar_cols); $i++) {
    if($ar_cols[$i]===null) continue;
    if($i==$last_key) $re_html .= '<div id="'. $preid.$ar_cols[$i]. '"> </div>';
    else $re_html .= '<span id="'. $preid.$ar_cols[$i]. '"> </span>';  }
  // adds the columns in JS (used in removeLists() to remove the next displayed lists when makes other selects)
  $re_html .= '<script type="text/javascript">var ar_cols = '.json_encode($ar_cols).'; var preid = "'. $preid. '";</script>';
}else echo $re_html;
?>

这里是ajax_select.js文件供参考

// Multiple select lists - http://coursesweb.net/ajax/
// function used to remove the next lists already displayed when it chooses other options
function removeLists(colid) {
  var z = 0;
  // removes data in elements with the id stored in the "ar_cols" variable
  // starting with the element with the id value passed in colid
  for(var i=1; i<ar_cols.length; i++) {
    if(ar_cols[i]==null) continue;
    if(ar_cols[i]==colid) z = 1;
    if(z==1) document.getElementById(preid+ar_cols[i]).innerHTML = '';
  }
}
// sends data to a php file, via POST, and displays the received answer
function ajaxReq(col, wval) {
  removeLists(col);           // removes the already next selects displayed
  // if the value of wval is not '- - -' and '' (the first option)
  if(wval!='- - -' && wval!='') {
    var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new     ActiveXObject('Microsoft.XMLHTTP');  // XMLHttpRequest instance
    var php_file = 'select_list.php';     // path and name of the php file
    // create pairs index=value with data that must be sent to server
    var  data_send = 'col='+col+'&wval='+wval;
    request.open("POST", php_file, true);           // set the request
    document.getElementById(preid+col).innerHTML = 'Loadding...';   // display a loading notification
    // adds a header to tell the PHP script to recognize the data as is sent via POST
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.send(data_send);            // calls the send() method with data_send
    // Check request status
    // If the response is received completely, will be added into the tag with id value of "col"
    request.onreadystatechange = function() {
      if (request.readyState==4) {
        document.getElementById(preid+col).innerHTML = request.responseText;
      }
    }
  }
}

任何想法?

我假设函数ajaxReq正在显示产品列表?在选择一个选项时你想要加载图像吗?

这将取决于当您从ajax获得列表时,您的图像url数据将显示在哪里。

我将添加一个id的选择元素,并使用jquery插入图像点击一个选项。假设您将图像url添加到值。

<select id="selectId">
    <option value="/img/product.png">Product List</option>
</select>
<div id="productImg">
</div>

<script>
$(function(){
    $("#selectId option").click(function(){
        $("#productImg").html('<a href="'+$(this).val()+'"><img src="'+$(this).val()+'"/>Product img</a>');
    });
});
</script>

在这里修改:https://jsfiddle.net/kqyfw2s2/