0

这是使用 php 和 ajax 从 mysql 数据库中检索名称的代码

更新代码

索引.php

    <html>
<head>
<script language="javascript">
    function showresult()
    {  
    var product_name = document.getElementById("searchval").value;   
    if(window.XMLHttpRequest)
    {
    XMLHttpRequestObject = new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
    XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }  
     XMLHttpRequestObject.open("POST", "search.php", true);   
     XMLHttpRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                    
     XMLHttpRequestObject.send("search_res=" + product_name);  
     XMLHttpRequestObject.onreadystatechange = function()  
    {  
        if(XMLHttpRequestObject.readyState == 4)
        {
            if(XMLHttpRequestObject.status == 200)
            {
                document.getElementById("displayresult").innerHTML = XMLHttpRequestObject.responseText;
            }
        }

    }  
    }
function throwval(obj)
{
    var sent_id = obj.id;
var v = document.getElementById(sent_id).value;
    var newp = document.createElement("p");     
var text = document.createTextNode(v);
newp.appendChild(text);
document.getElementById("getselected").appendChild(newp);  
}
function sendvalues()
{

var div_val = document.getElementById("getselected");
var str="|";
    for (i=0; i < div_val.getElementsByTagName("p").length; i++)
    {
    var paragraphs = div_val.getElementsByTagName("p");
    var pvalues = paragraphs.item(i).innerHTML;
str = str + pvalues + "|";
}   
window.location="send_data.php?str="+str;
}

    </script>
    </head>
    <body>
    <form method="post" name ="searchform" id="idsearchform" >
    <input type="text" name="search" id="searchval"/>
    <input type="button" name="starts" value="startsearch" onclick="showresult()"/>
    </form>
    <div id="displayresult">

    </div>
    <div id="getselected">
    Selected :
    </div>
    <form name="sendf" method="post" action="send_data.php">
    <input type="button" id="sendtophp" name="sendingval" value="next step" onclick="sendvalues()">
    </form>
    </body>
    </html>

搜索.php

$mysql_con = mysql_connect("localhost","root","") or die("Could not   connect".mysql_error());
$mysql_db = mysql_select_db("test",$mysql_con) or die("Unable to select db  ".mysql_error());
$keyword = mysql_real_escape_string($_POST['search_res']);
$search_q = mysql_query("Select * from products where pname like '%$keyword%'");
if(mysql_num_rows($search_q)!=0)
{
    while($result = mysql_fetch_array($search_q))
    {
        $name = $result['pname'];
        echo "<input type='button' name='resultname' id='$productid' value='$name' onclick='throwval(this)'><br/>";
    }
}

发送数据.php

<?php
$url = $_SERVER['REQUEST_URI'];
$exploded = explode('|',$url,-1);
$number = count($exploded);
?>
<html>
<body>
<table align="center" border="1">
<form method="post" action="send_data.php">
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
<?php
for($i=1;$i<$number;$i++)
{
    $p_name = $exploded[$i];
?>
<tr>
<td><input type="text" name="p_names" value="<?php echo $p_name; ?>" /></td>
<td><input type="text" name="quantity" /></td>
</tr>
<?php 
}
?>
<tr>
<td></td>
<td><input type="submit" name="send_request" value="Submit" /></td>
</tr>
</form>
</table>
</body>
</html>

我想将选定的结果发送到另一个 div,从那里我可以使用 php 或 javascript(没有 jquery)将选定的值发送到另一个文件。我怎样才能做到这一点。

更新:我已经使用 appendchild 成功地将数据从一个发送到另一个。我现在的问题是我添加的值是作为单个字符串添加的。我希望每个值都是分开的,以便我可以将其发送到 php(有点像购物车)。有任何想法吗?谢谢

I managed to send separate values using javascript by creating a new p attribute for every value that is selected. I can now display the values in send_data.php but i do not know how to send multiple values generated by the for loop through one form. Any ideas?

4

1 回答 1

0

I understand that you are trying to build a search form. The user can do multiple searches. The user may select a result from each search request. Each selected result is stored somewhere and send to another php script once the user is done with searching and selecting.

I would add a hidden field to the HTML and append the result not only to the DIV like you have done, but also to the value of the hidden field. Let's say the user selected productId 1,3,5 the value of the hidden field would be "1,2,3". In PHP you can explode the string and cast each element into an int and do whatever you want with it.

This approach assumes that the user clicks some kind of "I am done button" once he is done with searching and selecting, so that you can submit the form with the hidden field to the server.

The alternative is to send the selected value back to the server using an Ajax request as soon as the user selected a value.

于 2013-04-10T06:56:56.363 回答