AJAX函数不会将数据发送到PHP文件

时间:2014-09-16 11:17:03

标签: javascript php jquery ajax

我有一个提交表单,我不希望在提交后,页面刷新,所以我创建了一个AJAX函数,应该将数据发送到PHP文件,但问题是AJAX函数不发送变量PHP脚本。

index.html,格式为

<!-- FORM -->
<form name="form" id="form" method="post">
<div class="row"> 
<div class="col-md-12">
<!-- GAME -->
<select class="form-control" id="game-group" name="game" onchange="ChangeBackground();">
<option selected disabled>Select your Game...</option>
<option value="League_of_Legends">League of Legends</option>
<option value="Heartstone">Hearthstone</option>
</select>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12">
<!-- QUESTION -->
<div class="input-group" id="question-group">
<input type="text" class="form-control" name="question" id="question" placeholder="Start typing your question...">
<span class="input-group-addon">
<i class="glyphicon glyphicon-question-sign"></i>
</span>
</div>
</div>
</div>
<br>
<div class="row">
<!-- OPTIONS -->
<div class="form-group form-group-options col-md-12 col-sm-12 col-xs-12">
<div class="input-group input-group-option col-md-12 col-sm-12 col-xs-12" id="options-group">
<input type="text" name="option[]" id="option" class="form-control" placeholder="Options...">
<span class="input-group-addon input-group-addon-remove">
<span class="glyphicon glyphicon-remove"></span>
</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<!-- CHOICE -->
<div class="checkbox" id="choice-group">
<label>
<input type="checkbox" id="choice" name="choice" value="Yes">Allow multiple choice
</label>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<button type="button" class="btn btn-primary btn-lg pull-left" name="submit_button" id="submit_button" onclick="sendForm()" data-toggle="modal" data-target="#myModal">Create Poll</button>
</div>
</div>
</form>
带有AJAX功能的

magic.js

function sendForm()
{
var form = new FormData($('#form')[0]);
form.append('view_type','addtemplate');
$.ajax(
{
    type: "POST",
    url: "process.php",
    data: form,
    cache: false,
    contentType: false,
    processData: false,
    success:  function(data)
    {
        alert("GeGe");
    }
});
}

process.php(在查询中我只将一个变量插入数据库中以查看它是否有效,不要担心这个)

<?php

//Include configuration file
include('includes/config.php');

//Define variables
$game=$_POST['game'];
$question=$_POST['question'];
$option=$_POST['option'];
$choice=$_POST['choice'];

//Generate random number
$rand_value=rand();

//Create temporary folder
//mkdir($rand_value);

//Copy page of Ask Poll
//copy('page.php', $rand_value . '/page.php');
//rename($rand_value . '/page.php', $rand_value . '/index.php');

//Add data into database
mysql_connect($db_host, $db_username, $db_password) or die ("Errore di connessione!");
mysql_select_db($db_name) or die ("Impossibile selezionare database!");
//$sql1="CREATE TABLE `" . $rand_value . "` (Question VARCHAR(200), Options VARCHAR(200), Choice INT(11))";
//mysql_query($sql1) or die ("Impossibile eseguire la query!");

$sql2="INSERT INTO `675197851` (Question) VALUES ($question)";
mysql_query($sql2);
//Count number of Options available
//$count=count($option);

//for ($i=0; $i<($count-1); $i++)
//{
//  ${$sql . $i}="INSERT INTO `" . $rand_value . "` (Question, Options, Choice) VALUES ($question2, $option2[$i], $choice2)";
//  mysql_query(${$sql . $i});
//} 
?>

1 个答案:

答案 0 :(得分:-1)

如果您正在使用JQuery,那么请尝试这种解决方案。

function sendForm()
{
    $.post( "process.php", $( "#form" ).serialize())
    .done(function( data ) {
        alert( "Data Loaded: " + data );
    });
}