通过javascript单击按钮时将var传递给url

时间:2015-11-19 19:37:39

标签: javascript html

<input type="text" name="my_sld" style="width: 55%" value="">
<br />
<select name="my_tld" style="width: 20%">
    <option value="com" label="com"> .com </option>
    <option value="net" label="net"> .net </option>
    <option value="org" label="org"> .org </option>
</select>

<script>
var sld = my_sld();
var tld = my_tld();
var domain = sld + tld;
</script>

<button go to = http://example.com/check/ + domain + >

当我通过javascript点击按钮时,如何让my_sld + my_tld转到mysite?

例如= http://example.com/check?domain=thedomainname.net

2 个答案:

答案 0 :(得分:7)

你应该这样做..

首先向按钮提供任何ID:

<button id=“test” value=“click">

然后在javascript中你应该这样做:

<script>
$("#test").click(function(){
      var sld = my_sld();
      var tld = my_tld();
      var domain = sld+tld;
      window.location.href="http://example.com/check?domain="+domain;
 })
</script>

答案 1 :(得分:1)

<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";

$con=mysql_connect("localhost","$username","$password");
mysql_select_db("$dbname", $con);

$id=$_POST['id'];
$name=$_POST['name'];
$email=$_POST['email'];

mysql_query("INSERT INTO table(id, name, email)
VALUES ('$id', '$name', '$email')");

echo "New record created successfully";

?>

或使用不显眼且仍然纯粹的javascript

<script>
function redirect() {
    window.location = 'http://example.com/check/' + sld + tld;
}
</script>
<button onclick='redirect();'>