将表单值从PHP文件传递到另一个PHP文件

时间:2016-06-24 12:53:31

标签: javascript php html mysql forms

我在 home.php 中制作一个这样的弹出式表单:

to.analyze <- read.csv("FMS.to.analyze.csv") # read the data

to.analyze$r <- as.POSIXct(to.analyze$TOD) # do class change first and add it as a new column
levels(to.analyze$Year.Month)       # I didn't use Year.Month because of alphabetical levels.
to.analyze$Year.Month2 <- substr(to.analyze$r, 1, 7)
# make a new column, Year.Month2 by extracting POSIXct class data
short.sumRain <- aggregate(Precip_.mm. ~ Year.Month2, to.analyze, sum)

x.range <- as.POSIXct(c("2013-05-01", "2013-09-01"))   # decide the x-range on plot
x.month <- as.numeric( as.POSIXct(paste0( unique(to.analyze$Year.Month2), "-15")) )
# decide the day where boxplot and short.sumRain's points are. (I used 15th of every month)
   # (paste0() joins year-month to -day. as.numeric(as.POSIXct()) calculate the real value.)

par(mar=c(4, 4.2, 2.5, 4.2))                           # set margin (down, left, up, right)
plot(Moist_10cm ~ r, to.analyze, xlab = "Time", xaxt = "n", ylab = "Volumetric Water Content", 
     main = "FMS", type = "l", ylim = c(0, .3), xlim = x.range)      # set xlim = x.range
axis.POSIXct(1, at = seq(x.range[1], x.range[2], by = "months"), labels = TRUE, 
             format = "%Y-%m", las = 1, cex = 0.1)
# you can draw additional other depth data by succeeding code.
# lines(Moist_100cm ~ r, to.analyze, col = "gray")

# use boxplot()'s arguments add=T and at. (If you don't want transparency, please delete col = "#00000020")
boxplot(Moist_10cm ~ Year.Month2, to.analyze, boxwex = 900000, 
        at = x.month, axes = F, col = "#00000020", add = T)

par(new = TRUE)   ### HERE, the first xy-coordinates are reset.
plot(short.sumRain$Precip_.mm. ~ x.month, type = "o", col = "blue", 
     axes=F, ann=F, xlim = x.range)           # use the same xlim
axis(4)
mtext("Precip (mm)", side = 4, line = 2)

# If you needn't keep TOD and/or Year.Mont, you can overwrite it 
# (e.g., to.analyze$TOD <- as.POSIXct(to.analyze$TOD) ) instead of making a new column.

我填写表格。当我点击“添加”时按钮,它运行javascript函数。 submit.js 中的代码:

<script src="js/submit.js"></script>

.........
.........
.........

<div id="abc">
<!-- Popup Div Starts Here -->

<div id="popupContact">
<!-- Form -->

<form action="#" id="form" method="post" name="form">

    <input id="month" name="month" placeholder="MONTH" type="text">
    <a href="javascript:%20check_empty()" id="submit">ADD</a>

</form>

</div>
<!-- Popup Div Ends Here -->
</div>

我想在 insertdata.php 中运行查询,如下所示。它需要来自&#39;月&#39;的价值。

function check_empty() {
    if (document.getElementById('month').value == ""){
        alert("Fill column!");
    } else {
        document.getElementById('form').submit();   
        $.get("application/insertdata.php");
        return false;
    }
}

//Function To Display Popup
function div_show() {
document.getElementById('abc').style.display = "block";
}

//Function to Hide Popup
function div_hide(){
document.getElementById('abc').style.display = "none";
}

查询成功运行,并在我的表中添加了行。 &#39; TEST&#39;列添加了&#39; xxx&#39;。但是在&#39; MONTH&#39;列,它不生成任何值,只是空。

那么,如何获得这个月的#39;值? 谢谢。

3 个答案:

答案 0 :(得分:4)

由于您使用的是JavaScript / jQuery,因此您无需在HTML中使用内联代码,因此请从内置JavaScript中删除内容:

<script src="js/submit.js"></script>

.........
.........
.........

<form action="#" id="form" method="post" name="form">

<input id="month" name="month" placeholder="MONTH" type="text">
<a href="#" id="submit">ADD</a>

</form>

更清洁,不是吗?您没有在函数调用中传递任何数据,这可能会给您带来问题。

现在,您可以在JavaScript / jQuery中更简单地设置我们捕获点击事件并通过$.post传递数据:

$('#submit').click(function(event) {
    event.preventDefault(); // prevent the default click action
    var month = $('#month').val();
    if('' == month) {
        alert('fill the column!');
    } else {
        $.post("application/insertdata.php", {month: month}); // notice how the data is passed
    }
});

到目前为止,这么好,代码更紧凑,更易读,实际上将数据从表单发布到AJAX调用。

最后PHP,测试变量month是否设置正确:

<?php 
    require("phpsqlajax_dbinfo.php");

    $conn = mysqli_connect('localhost', $username, $password, $database);

    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    } 

    if(isset($_POST['month'])) {
        $data = $_POST['month'];
        $monthstring = mysqli_real_escape_string($conn, $data);
        $sql = "INSERT INTO `databasea`.`tablea` (`MONTH`, `TEST`) VALUES ('". $monthstring ."', 'xxx');";
        mysqli_query($conn, $sql);
    }

    mysqli_close($conn);
?>

注意 :我担心您的网页上可能有多个这样的表单,而且您可能会复制不起作用的ID而且需要删除重复的ID。如果是这种情况,我写的jQuery代码需要更改。这是一种方法:

$('a').click(function(event) {
    event.preventDefault(); // prevent the default click action
    var month = $(this).prev('input').val(); // get the input next to the link
    if('' == month) {
        alert('fill the column!');
    } else {
        $.post("application/insertdata.php", {month: month}); 
    }
});

正如我在评论Little Bobby中所说, your script is at risk for SQL Injection Attacks. 了解preparedMySQLi语句。即使escaping the string也不安全!更改为准备好的语句将使您的代码更清晰,更安全。

答案 1 :(得分:2)

嗨使用$data = $_POST['month'];

isset将返回truefalse而不是月份值

答案 2 :(得分:2)

替换

$data = isset($_POST['month']);

通过

if(isset($_POST['month'])) {
   $data=$_POST['month'];
}