表格检查信息&插入同一页面

时间:2016-06-01 07:36:02

标签: javascript php html forms

所以我有一份常规表格

<form action="includes/send.php" method="post" onsubmit="return isValidForm()" />>

<h1>Opgeven workshops</h1>

<label for="name">Voornaam:</label>
<input type="text"  autocomplete="off" id="name" name="firstname">
<label class="choice" data-id="1"><input type="checkbox" name="group1" value="use your apple1">use your apple<span class="left" ></span>
</label>---more stuff more stuff more stuff--

现在我提交表格,我希望以这样的形式显示用户填写的信息

$f_name = $_POST['firstname'];
$l_name = $_POST['lastname'];

U hebt zich ingeschreven bij: <br />
Eerste workshop : <?php echo $first; ?><br />
Tweede workshop : <?php echo $second; ?><br />
Klopt dit?

<button type="submit" onclick="send()">Ja</button>
<button type="submit" onclick="noSend()">nee</button>

当用户点击send时,它会将上一个表单中的信息发送到查询,以将其插入数据库。我试图这样做而不必另外制作一个隐藏的形式&#39;再次提交它,因为当你可以让脚本等待&#39;并在按下按钮时继续使用脚本/插入功能。

我尝试设置变量$submit= false;并在send函数内(在javascript中)将submit设置为true,但这似乎不起作用因为它在不按下按钮的情况下自动将变量设置为true。

function send(){
<?php $submit = true ?>
var submit = <?php echo $submit ?>;
console.log(submit);
}
if($submit){
    echo 'submitted';
} else {
    echo 'not true';
}

1 个答案:

答案 0 :(得分:0)

在你的php端调用你的Javascript&#39; send()&#39;时传递值。功能

<?php 
     $first = "First course";
     $second = "Second course"; 
?>


U hebt zich ingeschreven bij: <br />
Eerste workshop : <?php echo $first; ?><br />
Tweede workshop : <?php echo $second; ?><br />
Klopt dit?

<!-- pass the required values into the function, this is just a basic implementation you could also use a loop to fill in the values  -->

<button type="button" onclick="send(true, '<?php echo $first ?>', '<?php echo $second ?>')">
     Ja
</button> 
<button type="button" onclick="send(false)">
     nee
</button>

对于接收功能,你可以实现类似这样的东西

<script type="text/javascript">
    function send(submit){      
        //Get all arguments passed except the first variable, the submit boolean
        var listOfVariablesToPost = Array.prototype.slice.call(arguments,1); 
        if(submit){
            console.log("post");
            console.log(listOfVariablesToPost);
            /* Do POST here either by using XMLHttpRequest or jQuery AJAX/POST (Or any other way you like)*/
            /* XMLHttpRequest: http://stackoverflow.com/questions/9713058/sending-post-data-with-a-xmlhttprequest */
            /* jQuery POST https://api.jquery.com/jquery.post/ */
        }else{
            console.log("No post")
            /* Don't post and do whatever you need to do otherwise */
        }
    }
</script>

这是一个非常简单的实现,但我希望它有所帮助。