使用AJAX提交选择值PHP

时间:2017-01-24 00:03:12

标签: php ajax forms

我有这个代码,但我无法在不刷新页面的情况下更改提交的变量。你能帮我用AJAX吗?

代码: .................................................. ..............................

select Person, Date, Action,
       Action / sum(action) over (partition by person) as p_action
from t;

1 个答案:

答案 0 :(得分:0)



<script src="jquery-3.1.1.min.js" type="text/javascript"></script>

<script type="text/javascript" src="javascript.js">
	//get value of the select box
    var device = document.getElementById('device').value;

	//post ajax jquery command
    $.post(<?php echo "'".$_SERVER['PHP_SELF']."'"; ?>, {
            device:device
    }, function(result){
            alert(result);
    });
	//it will send post data by post method
</script>
&#13;
Please Download the jquery file and put beside the php file

<a href="https://jquery.com/">https://jquery.com/</a>


<form name="devices" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <select name="device" id="device">

        <option value="i1">i1</option>
        <option value="i2">i2</option>
     </select>

</form>

<?php

$device = "";
if(isset($_POST['device'])) {
$device = $_POST['device'];

}

switch ($device) {

    case 'i1':
    $w = 50;
    break;
    case 'i2':
    $w = 100;

    break;
    default:
    $w = 50;        
        break;
}

?>
<div style="width: <?=$w?>px; height: 100px;background-color: black;"></div>
&#13;
&#13;
&#13;