2

I am posting data to PHP from jQuery using data from an HTML form.

Here is the jQuery line that sends the POST

$.post("InsertNewQuestion.php", $("Create_Question_Form").serialize());

Here is the PHP code

<?php
$con = mysql_connect("localhost","root","");

mysql_select_db("Quizzes",$con);

$Quiz_Name = $_POST['Question'];
echo $Quiz_Name;
$Option_1 = $_POST['Option1'];
echo $Option_1;
$Option_2 = $_POST['Option2'];
echo $Option_2;
$Option_3 = $_POST['Option3'];
echo $Option_3;
$Option_4 = $_POST['Option4'];
echo $Option_4;
$Option_5 = $_POST['Option5'];
echo $Option_5;

$rowIDList = mysql_query("SELECT rowID FROM TestQuiz");

$ColumnValues = array();

$CurrentGreatestRowID = -1;

$LCV = 1;

while($row1 = mysql_fetch_assoc($rowIDList)) {

    if ($CurrentGreatestRowID < $row1['rowID']) {
        $CurrentGreatestRowID = $row1['rowID'];
    }
    $LCV++;  
}   

$CurrentRowID = $CurrentGreatestRowID+1;        

$sql = "INSERT INTO TestQuiz (rowID,Quiz_Name,Option_1,Option_2,Option_3,Option_4,Option_5,Option_1_Votes,Option_2_Votes,Option_3_Votes,Option_4_Votes,Option_5_Votes)
VALUES(".$CurrentRowID.",'".$Question."','".$Option1."','".$Option2."','".$Option3."','".$Option4."','".$Option5."',0,0,0,0,0);";

if (mysql_query($sql,$con)) {
    echo "Inserted values";
}
else {
    echo ("Could not insert values: ". mysql_error());
}

mysql_close($con);
?>

Here is the HTML form

<form id="Create_Question_Form" action="" method="POST">
Question Name: input id="Question" class="Create_Question_Text_Box" type="text" name="Question_Name"><span id="Invalid_1"></span><br>
Option 1: input id="Option1" class="Create_Question_Text_Box" type="text" name="Option_1"><span id="Invalid_2"></span><br>
Option 2: input id="Option2" class="Create_Question_Text_Box" type="text" name="Option_2"><span id="Invalid_3"></span><br>
Option 3: input id="Option3" class="Create_Question_Text_Box" type="text" name="Option_3"><span id="Invalid_4"></span><br>
Option 4: input id="Option4" class="Create_Question_Text_Box" type="text" name="Option_4"><span id="Invalid_5"></span><br>
Option 5: input id="Option5" class="Create_Question_Text_Box" type="text" name="Option_5"><span id="Invalid_6"></span><br>
input type="Submit" id="Question_Submit" value="Create Question"></input>
</form>
10
  • The question is that the data is not being sent to InsertNewQuestion.php. Although, InsertNewQuestion.php is still called the data is not being sent so an empty row is being created. Commented Feb 20, 2013 at 15:23
  • 5
    Shouldn't it be $("#Create_Question_Form").serialize() ? Note the # Commented Feb 20, 2013 at 15:24
  • I made the change it still does not work, (only empty row is still being created). Commented Feb 20, 2013 at 15:26
  • Is there a reason you cannot use an auto-increment for the rowID? Commented Feb 20, 2013 at 15:28
  • 1
    And you have an sql injection hole that would break your query if any of the fields contain a '. Commented Feb 20, 2013 at 15:29

2 Answers 2

1

Your form selector should be #Create_Question_Form. Note the # indicating that this is an element ID.

$("#Create_Question_Form").serialize()

Update

You are accessing the $_POST values by ID instead of name. Try this:

$Option_1 = $_POST['Option_1'];
$Option_2 = $_POST['Option_2'];
// etc
Sign up to request clarification or add additional context in comments.

2 Comments

I already fixed that error still it does not work, (only empty row is being created).
It worked. The problem was that I didnt update the Options_1, Option_2 variable names in the line $sql = "INSERT INTO TestQuiz (rowID,Quiz_Name,Option_1,Option_2,Option_3,Option_4,Option_5,Option_1_Votes,Option_2_Votes,Option_3_Votes,Option_4_Votes,Option_5_Votes) VALUES(".$CurrentRowID.",'".$Question."','".$Option1."','".$Option2."','".$Option3."','".$Option4."','".$Option5."',0,0,0,0,0);"; Thanks everyone for the help! Great Response time!.
0
input type="Submit" id="Question_Submit" value="Create Question"></input>
<input type="Submit" id="Question_Submit" value="Create Question" />

You seem t be missing a bracket

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.