upload a particular picture instead of submitting full form - javascript

I have a form name editdata.php. around 10 html controls(text box,file,select) using. I have a file control their picture of user is displaying and below this a file control is attached to change picture. I want that when a user browse and then change picture picture will automatically changed but form will not submit. so is this possible to upload picture without submitting the full form.
<html>
<head>
<meta charset="UTF-8">
<html lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1"> <!--requuired for bootstrap -->
<link rel="stylesheet" href="Bootstrap/bootstrap.min.css" /> <!--bootstrap files -->
<script src="Bootstrap/jquery.min.js"></script>
<script src="Bootstrap/bootstrap.min.js"></script>
<style>
.error
{
color: red;
}
</style>
<title></title>
</head>
<body>
<div class="container">
<h1>Login Form</h1>
<h2><?php if(isset($_REQUEST['msg'])) echo $_REQUEST['msg'] ?></h2> <!-- Successful messgaeg if chnaged password -->
<form action="index.php" method="post" onsubmit="return checkData()" role="">
<div class="form-group">
<label>Username*</label>
<div><input type="text" class="form-control" id="uname" value="<?php if(isset($un)) echo $un; ?>" name="uname" onblur="setTimeout(function abc(){check_login_data('uname erruname blur', 'Username Must Be Entered')},130)" onfocus="check_login_data('uname erruname focus')" placeholder="Enter Username" /> <!-- textbox for username and checking value for cookie -->
</div>
</div>
<span class="error" id="erruname"> <?php if (isset($errorforusrnam)) echo $errorforusrnam; ?> </span> <!-- set a span for displaying error on emplty textbox -->
<div class="form-group">
<label>Password*</label>
<div><input type="password" class="form-control" id="password_id" name="upass" value="<?php if(isset($up)) echo ($up); ?>" placeholder="Enter Password" onblur="setTimeout(function abc(){check_login_data('password_id errpassword blur','Password Should Entered')},130)" onfocus="check_login_data('password_id errpassword focus')" /> <!-- textbox for Password and checking value for cookie -->
</div>
</div>
<span class="error" id="errpassword"> <?php if(isset($errorforpwd)) echo $errorforpwd ?> </span> <!-- set a span for displaying error on emplty textbox -->
<?php
if(isset($_SESSION['counter']) && $_SESSION['counter']>3) //captcha will show if user attempts more then 3
{
echo "<div class=form-group >"; //div for captcha
echo "<label for=captcha >Captcha*</label>";
echo "<div id=captcha_id>";
echo "<img id=imgCaptcha class=img-responsive src=captcha/create_image.php >";
echo '</div>';
echo '<div>';
echo "<div>";
echo "<input id=txtCaptcha class=form-control type=text name=txtCaptcha value='' placeholder='Enter Verification Code' />";
echo "<div><input class=btn btn-default type=button onclick=change_captcha() value=Reload /></div>";
echo "</div>";
echo "</div>";
echo "</div>"; //end of div
echo "<span class =error id=errco>"; //this will show error of div
if(isset($error))
{
echo $error;
}
echo '</span>';
}
?>
<div class="checkbox">
<label><input type="checkbox" name="chq" value="">Remember Me</label>
</div>
<div>
<input type="submit" name="sub" value="Login"class="btn btn-default" />
<a href="signup.php" />Signup</a>
<a href="forget_password.php" >Forgot Password</a>
</div>
<!-- <span id="span" ></span> span for showing output -->
</form>
</div>
</body>
</html>
and yeah all work will must have in single form...not is two forms. here I used two forms but I want to have a single form

GO through this liThis may Help You,
Ajax upload image on form showing thumbnail

Related

Iframe loading parent page on js function call

I am trying to get an iframe to run js on my site
the iframe page is as follows (some php omitted for relevance):
<?php
function createModeratorFormPart($moderator_email = null, $modNumber = 5){
echo "<script src=\"frontend.js\"></script>"; //implement adding moderators dynamically and such
echo "<fieldset id=\"moderator\">";
echo "<button onClick=\"addModerator()\">Add a moderator</button>";
if($moderator_email == null){ //if we have no moderators set
echo "<div>";
echo "<input class=\"addCommunityInput\" type=\"email\" name=\"moderator[$i]\" placeholder=\"Moderator Email\">";
echo "<button onClick=\"dropModerator(this)\">-</button>";
echo "</div>";
}else{
for($i = 0; $i < count($moderator_email); $i++){
echo "<div>";
echo "<input class=\"addCommunityInput\" type=\"email\" name=\"moderator_email[$i]\" placeholder=\"Moderator Email\" value=\"" . $moderator_email[$i] . "/>";
echo "<button onClick=\"dropModerator(this)\">-</button>";
echo "</div>";
}
}
echo "</fieldset>";
}
?>
<html>
<head>
<link rel="stylesheet" href="/web/addOfferingStyles.css" type="text/css" />
<script src="/web/frontend.js"></script>
</head>
<body>
<?php if(isset($_GET['success'])){
echo '<p>Offering successfully created</p>';
}else{
echo "<p>$retCode</p>";
}
?>
<h1 id="addCompOff">New Community Period</h1>
<form method="post" id="addCommunityForm" action="<?php echo 'https://theprepapp.com'. $_SERVER['PHP_SELF'];?>">
<input type="hidden" name="formSubmit" value="1"></input>
<input class="addCommunityInput" type="text" name="name" placeholder="Offering Name" />
<input class="addCommunityInput" type="text" name="description" placeholder="Description" />
<input class="addCommunityInput" type="text" name="location" placeholder="Location & Time Information" />
<input class="addCommunityInput" type="number" name="peopleLimit" placeholder="Student Capacity" />
<input class="addCommunityInput" type="text" name="moderator" placeholder="Moderator Names(s)" />
<?php createModeratorFormPart(); ?>
</fieldset>
<div>
<p>Days Offered:</p>
<div class="control-group">
<label class="control control-checkbox">
Monday
<input name="monday" type="checkbox"/>
<div class="control_indicator"></div>
</label>
<label class="control control-checkbox">
Tuesday
<input name="tuesday" type="checkbox"/>
<div class="control_indicator"></div>
</label>
<label class="control control-checkbox">
Wednesday
<input name="wednesday" type="checkbox" />
<div class="control_indicator"></div>
</label>
<label class="control control-checkbox">
Thursday
<input name="Thursday" type="checkbox" />
<div class="control_indicator"></div>
</label>
<label class="control control-checkbox">
Friday
<input name="Friday" type="checkbox" />
<div class="control_indicator"></div>
</label>
</div>
<input type="submit" value="Submit">
</form>
</body>
</html>
I am trying to run this function:
function addModerator(){
var fieldset = document.getElementById("moderator");
//create encompassing div so that deletion of the moderator can be deleted easily
var div = document.createElement("div");
div.setAttribute("className", "moderator");
//create label
var label = document.createElement("Label");
label.appendChild(document.createTextNode("Moderator"));
div.appendChild(label);
//create text field
var input = document.createElement("Input");
input.setAttribute("placeholder","Moderator Email");
input.setAttribute("type","email");
div.appendChild(input);
//create the delete button
var button = document.createElement("button");
button.setAttribute("value","-");
button.setAttribute("onClick", "dropModerator(this)");
div.appendChild(button);
//add the div to the fieldset
fieldset.appendChild(div);
}
When I run this through the onClick event, the console does not show any JS errors: however, the iframe document becomes the document of the parent page (even though the iframe src attribute is not changed)
(linked to here, though I can't imagine how it would be relevant: https://github.com/articDrag0n/PrepApp/blob/master/web/teacher/teacher.php) How is this happening?
The problem was the form submission
Everytime I clicked on a button , the form would submit (and due to the form handling code, would go to the parent page);
solution was to add this line button.setAttribute("type", "button");
or, if your doing it in html <button type="button">
Ended up being a clone of this one:Can I make a <button> not submit a form?

Contact form doesn't send predefined values

I'm currently making a form wich works, but it doesn't send predefined values.
I have a few fields that I have predefined such as the user and total amount.
So it does send the values I enter, but not the ones that I have predefined
Can someone please help me, so that this thing can work as it should?
<?php require('includes/config.php');
//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: index.php'); }
?>
<html>
<head>
<title>*****</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!--[if lte IE 8]><script src="siteassets/assets/js/ie/hsiv.js"></script><![endif]-->
<link rel="stylesheet" href="siteassets/assets/css/main.css" />
<!--[if lte IE 8]><link rel="stylesheet" href="siteassets/assets/css/ie8.css" /><![endif]-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#next').keyup(calculate);
$('#nextprice').keyup(calculate);
$('#current').keyup(calculate);
$('#currentprice').keyup(calculate);
});
function calculate(e)
{
$('#total').val($('#next').val() * $('#nextprice').val() + $('#current').val() * $('#currentprice').val());
}
</script>
</head>
<body class="landing">
<div id="page-wrapper">
<!-- Header -->
<header id="header" class="alt">
<nav id="nav">
<ul>
<li>
Menu
<ul>
<li>Current Project</li>
<li>Next Projects</li>
<li>Previous Projects</li>
<li>Who are we?</li>
</ul>
</li>
<li>Tickets</li>
<li><a href='logout.php'>Logout</a></li>
</ul>
</nav>
</header>
<!-- Banner -->
<section id="banner">
<h2>********</h2>
<p>*******</p>
<ul class="actions">
<li>Current Project</li>
<li>Next Projects</li>
<li>Previous Projects</li>
<li>Who are we?</li>
<li>Tickets</li>
</ul>
</section>
<!-- Main -->
<section id="main" class="container 75%">
<div class="box">
<?
if($_SERVER['REQUEST_METHOD']=="POST")
{
if(strlen($_POST['name2']) == 0)
{ $error_msg ="- Please, provide us with your name.<br>"; }
if(!empty($error_msg))
{
//Field error
echo "<b>Your message can't be send due to the following reason:</b><br><br>";
echo $error_msg;
echo "<br>Click on <a href='javascript:history.back(1)'>Go back</a> and provide us with your name.<br><br>";
}
else
{
$recipient = "*********";
$subject = "******";
$header = "From: " . $_POST['uwemail'] . "\n";
$mail_body = "Contact script werd op " . date("d-m-Y") . " om " . date("H:i") . " uur uitgevoerd.\n";
$mail_body .= "De volgende persoon zou graag kaarten bestellen:\n\n";
$mail_body .= "Naam: " . $_POST['name2'] . "\n";
$mail_body .= "Met als kaartnummer: " . $_POST['card2'] . "\n";
$mail_body .= "Aantal kaarten voor de lopende productie: " . $_POST['current2'] . "\n";
$mail_body .= "Aantal kaarten voor de komende productie: " . $_POST['next2'] . "\n";
$mail_body .= "Voor een totaal: " . $_POST['total2'] . "\n";
$mail_body .= "\n\n -- Einde van het contact bericht --";
mail($recipient, $subject, $mail_body, $header);
print "<b>IMPORTANT!</b>";
print "<br><br>Thank you for your reservation.";
print "<br><br>Please note that this will be final upon receipt of payment of the total amount of";
print " POST TOTAAL";
print "EUR to Mazzini Theatre Productions";
print "<br><br>Confirmation of reservation and payment instruction details will be sent to you via email.";
print "<br><br>We are looking forward to meet you.";
}
}
else
{
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST" name="contact">
<div class="row uniform 50%">
<div class="6u 12u(mobilep)">
Your personal card number
<input type="text" name="card2" id="card" value="<?php echo $_SESSION['username']; ?>" placeholder="Card Number" disabled/>
</div>
<div class="6u 12u(mobilep)">
Please enter your name. (mandatory)
<input type="text" name="name2" id="name" value="" placeholder="Your name" />
</div>
</div>
<div class="row uniform 50%">
<div class="6u 12u(mobilep)">
Current Project - Smile
<input type="text" name="current2" id="current" value="" placeholder="How many tickets would you like?" />
</div>
<div class="6u 12u(mobilep)">
Next Project - Sand
<input type="text" name="next2" id="next" value="" placeholder="How many tickets would you like?" />
</div>
</div>
<div class="row uniform 50%">
<div class="6u 12u(mobilep)">
<input type="hidden" id="currentprice" value="10" />
</div>
<div class="6u 12u(mobilep)">
<input type="hidden" id="nextprice" value="10" placeholder="" />
</div>
</div>
<div class="6u 12u(mobile)">
<input name="uwemail" placeholder="Email" type="hidden" value="**THIS WORKS**"/>
</div>
<div class="6u 12u(mobilep)">
Total price.(In EUR)
<input type="text" name="total2" id="total" value="" disabled/>
</div>
</div>
<div class="row uniform">
<div class="12u">
<ul class="actions align-center">
<li><input type="submit" name="submit"value="Place Order"/></li>
</ul>
</div>
</div>
</form>
<?php
}
?>
</div>
</section>
<!-- Footer -->
<footer id="footer">
<ul class="copyright">
<li>© **DISCLAIMER**</li>
</ul>
</footer>
</div>
<!-- Scripts -->
<script src="siteassets/assets/js/jquery.min.js"></script>
<script src="siteassets/assets/js/jquery.dropotron.min.js"></script>
<script src="siteassets/assets/js/jquery.scrollgress.min.js"></script>
<script src="siteassets/assets/js/skel.min.js"></script>
<script src="siteassets/assets/js/util.js"></script>
<!--[if lte IE 8]><script src="siteassets/assets/js/ie/respond.min.js"></script><![endif]-->
<script src="siteassets/assets/js/main.js"></script>
</body>
</html>
For your calculated fields you set attribute disabled. Fields with this attribute are not sent by form. Remove that attribute. You can set in that place readonly attribute
Forms do not send disabled fields. You alternatively can use readonly instead of disabled.
your hidden inputs are missing name= that's why they are not sent

Use PHP to display user inputs

What I'm trying to do is to use PHP to display the user inputs on a separate page, but when I process the form none of the input values display. I'm having to use javascript for input validation and that works fine but the PHP page is not working.
Here is my code:
HTML and Javascript
survey.php:
<!DOCTYPE html>
<html>
<head>
<title>Survey Page</title>
<link rel="stylesheet" type="text/css" href="mystylepage.css" />
<script type="text/javascript">
function validate()
{
if (document.information.name.value == "" )
{
alert("Please provide your name!" );
document.information.value.focus();
return false;
}
if (document.information.email.value == "")
{
alert("Please provide an email!");
document.information.value.focus();
return false;
}
if (document.information.major.value == false)
{
alert("Please enter your major");
document.information.value.focus();
return false;
}
return true;
}
</script>
</head>
<div id="bodycolor">
<body>
<div id="container">
<div id="menu">Menu:
<a class="menu" href="index.htm">Home</a> | <a class="menu"
`href="faculty.htm">Faculty</a> |
<a class="menu" href="courses.htm">
Courses</a> | <a class="gradecalculator" href="gradecalculator.htm">Grade
Calculator</a> | <a class="survey" href="survey.htm">Survey</a>
</div>
</div>
<div id="header">
<h1><center>CSE Center for Health Information Technology</center></h1>
<br>
<div id="links">
<center><a href="https://www.kennesaw.edu/"><img src="KSU Logo.jpg"
alt="Logo" style="width:100px;height:100px;" /></a></center>
<br>
<center><a href="http://ccse.kennesaw.edu/">College of Computing and
Software Engineering</a></center>
</div>
</div>
<h1>Please enter you information below</h1>
<form name="information" method="POST" action="action.php"
onsubmit="return
validate();">
<table width="500" border="0">
<tr>
<td width="150" bgcolor="#99CCFF"><strong>Name</strong></td>
<td><input type="text" name="name" size="20" maxlength="20" /></td>
</tr>
<tr>
<td bgcolor="#99CCFF"><strong>Email</strong></td>
<td><input type="text" name="email" size="20" maxlength="20" /></td>
</table>
<p> Major:
<input name="major" type="radio" value="Information Technology" />
Information Technology
<input name="major" type="radio" value="Software Engineering" />
Software Engineering
<input name="major" type="radio" value="Computer Science" />
Computer Science</p>
<p>
<input type="submit" value="Process" />
</p>
<p id="msg"></p>
</form>
</body>
</html>
Here is the PHP page:
action.php
<!DOCTYPE html>
<html>
<head>
<title>Survey Information Processing</title>
</head>
<body>
<h1>Form data</h1>
<p>Name: <?php echo $_POST["name"]?></p>
<p>Email: <?php echo $_POST["email"]?</p>
<p>Major: <?php echo $_POST["major"]?</p>
</body>
</html>
I'm totally new to PHP, so any help is greatly appreciated!
Your form method should be POST.
<form name="information" method="POST" action="action.php" onsubmit="return validate();">
And add a name to the submit button:
<input type="submit" value="Process" name="submit" />
Also, you could add this if condition:
<?php
echo "<pre>";
print_r($_POST); // Check if you're getting any POST data
echo "</pre>";
?>
<?php if (isset($_POST['name'])) { ?>
<h1>Form data</h1>
<p>Name: <?php echo $_POST["name"]; ?>
</p>
<p>Email: <?php echo $_POST["email"]; ?>
</p>
<p>Major: <?php echo $_POST["major"]; ?>
</p>
<?php } ?>
Hope this helps.
So the issue is your method is get but you're trying to access through post
Change
method="GET"
to
method="POST"
On your form.

Dynamic html form values are not being stored in database

I am developing a game portal in which professor should be able to add any type of questions in a game. I have created the question type(multiple choice or descriptive ) functions in form.php and i am calling them in my main file. First of all in the loop I am calling main box(simple html box) in which i have to add the question. And its working fine. Now i have to store the dynamically changed boxes values in database. But i don't know where i am wrong. Following is my form.php in which i am creating forms to call in the main file.
<?
$i=$_post['i'];
$_SESSION["input_type"][$i]= $_POST["type"];
if($_SESSION["input_type"][$i]==1)
{
form($i);
}
elseif($_SESSION["input_type"][$i]==2)
{
form1();
}
function form($i)
{
?>
<div class="control-group">
<div class="controls">
<textarea class="large m-wrap" placeholder=" Statement " cols="50"rows="3" name="statement<?echo $i;?>" style="text-align:center;" id="statement<?echo $i;?>"></textarea>
</div>
</div>
<div class="name">
<input name="option<?echo $i.'1';?>" id="option<?echo $i.'1';?>" placeholder="Option 1" style="width:170px;" type="text"/>
<input name="option<?echo $i.'2';?>" id="option<?echo $i.'2';?>" type="text" style="width:170px;" placeholder="Option 2"/>
<input name="option<?echo $i.'3';?>" id="option<?echo $i.'3';?>" type="text" style="width:170px;" placeholder="Option 3"/>
<input name="option<?echo $i.'4';?>" id="option<?echo $i.'4';?>" type="text" style="width:170px;" placeholder="Option 4"/>
</div>
<div class="control-group">
<div class="controls">
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
<label class="radio">
<input type="radio" name="option<?echo $i.'1';?>_default" id="option<?echo $i.'1';?>_default" />
Option 1
</label>
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
<label class="radio">
<input type="radio" name="option<?echo $i.'2';?>_default" id="option<?echo $i.'2';?>_default" checked />
Option 2
</label>
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
<label class="radio">
<input type="radio" name="option<?echo $i.'3';?>_default" id="option<?echo $i.'3'?>_default" />
Option 3
</label>
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
<label class="radio">
<input type="radio" name="option<?echo $i.'4';?>_default" id="option<?echo $i.'4';?>_default" />
Option 4
</label>
</div>
</div>
<?php
}
//end
?>
<?php
function form1()
{ ?>
<div class="control-group" id ="field" name="field">
<label class="control- label">Answer</label>
<div class="controls">
<input type="text" placeholder="Answer" id ="ans" name="ans" class="m-wrap small" />
</div>
</div>
<?
}
?>
This is my main file in which i am cakking the form.php functions to add questions
<!-- BEGIN PAGE -->
<div class="page-content">
<form action="storeGame.php" method="POST">
<div class="control-group">
<label class="control-label">Game Name</label>
<div class="controls">
<input type="text" id="game_name" name="game_name" placeholder="Enter Game Name" class="m-wrap large" />
</div>
</div>
<!-- BEGIN BORDERED TABLE PORTLET-->
<?
$q_no=5;
for ($i=0;$i<$_SESSION["q_inc"]; $i++)
{
?>
<div class="portlet box yellow">
<div class="portlet-title">
<h4><i class="icon-coffee"></i>#<?echo $i+1;?> </h4>
<div class="tools">
</div>
</div>
<div class="portlet-body">
<table class="table table-bordered table-hover">
<thead>
</thead>
<tbody>
<form action="newGame.php" method="POST" id="input_type" name="input_type">
<div class="control-group">
<label class="control-label" > Add Input</label>
<div class="controls">
<select class="medium m-wrap question_type" data-question-no="<?echo $i;?>" tabindex="1" id="type<?echo $i;?>" name="type<?echo $i;?>">
<option value="">Input Type</option>
<option value="1">Multiple Choice</option>
<option value="2">Input Field</option>
</select>
</div>
<div id="answer_no_<?php echo $i ?>"></div>
</div>
</form>
</tbody>
</table>
</div>
</div>
<script>
$(document).ready(function(){
$('.question_type').change(function(){
var question_no=$(this).attr('data-question-no');
$.ajax({
url: "form.php",
type:'post',
data:{
type:$(this).val(),
i:question_no
},
success:function(data){
$('#answer_no_'+question_no).html(data);
}
});
});
});
</script>
<?
}
?>
<!-- END BORDERED TABLE PORTLET-->
<!-- BEGIN PAGE CONTAINER-->
<div class="container-fluid">
<!-- BEGIN PAGE HEADER-->
<div class="row-fluid">
<div class="span12">
<!-- BEGIN STYLE CUSTOMIZER -->
<div class="color-panel hidden-phone">
<div class="color-mode-icons icon-color"></div>
<div class="color-mode-icons icon-color-close"></div>
<div class="color-mode">
<p>THEME COLOR</p>
<ul class="inline">
<li class="color-black current color-default" data-style="default"></li>
<li class="color-blue" data-style="blue"></li>
<li class="color-brown" data-style="brown"></li>
<li class="color-purple" data-style="purple"></li>
<li class="color-white color-light" data-style="light"></li>
</ul>
<label class="hidden-phone">
<input type="checkbox" class="header" checked value="" />
<span class="color-mode-label">Fixed Header</span>
</label>
</div>
</div>
<!-- END BEGIN STYLE CUSTOMIZER -->
<!-- BEGIN PAGE TITLE & BREADCRUMB-->
<h3 class="page-title">
</h3>
<!-- END PAGE TITLE & BREADCRUMB-->
</div>
</div>
<!-- END PAGE HEADER-->
<!-- BEGIN PAGE CONTENT-->
<div class="row-fluid">
<div class="span12" >
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
<input type="hidden" id="session" data="#Request.RequestContext.HttpContext.Session['questNo']" />
<!-- <a class="btn green" type="submit" ><i class="icon-plus" ></i></a> -->
<button type="submit" id="add_q" name="add_q" class="btn green"><i class="icon-plus"></i></button>
</form>
</div>
<!-- END PAGE CONTENT-->
</div>
<!-- END PAGE CONTAINER-->
<button type="submit" class="btn yellow btn-block" id="getGames" name="getGames" class="btn green">Create Game <i &nbsp class="m-icon-big-swapright m-icon-white"></i></button>
</form>
</div>
</div>
<!-- END PAGE -->
And below is my storeGame.php file in which i am getting values of question fields and saving in the database but the dynamic box values are not being saved but all other box values are being saved.
<?
session_start();
include_once("../Includes/db_connection.php");
$lecturer_id = $_SESSION["lecturer_id"];
$game_name=mysql_real_escape_string($_POST['game_name']);
echo $_SESSION["lecturer_id"];;
echo $game_name;
mysql_query("insert into games(game_name, lecturer_id) values ('$game_name', '$lecturer_id')");
for($i=0;$i<$_SESSION["q_inc"];$i++)
{
$input_type = mysql_real_escape_string($_POST['type'.$i]);
if($input_type=='1')
{
$question= $_POST['statement'.$i];
$val1= $_POST['option'.$i.'1'];
$val2= $_POST['option'.$i.'2'];
$val3= $_POST['option'.$i.'3'];
$val4= $_POST['option'.$i.'4'];
$default1= $_POST['option'.$i.'1'.'_default'];
$default2= $_POST['option'.$i.'2'.'_default'];
$default3= $_POST['option'.$i.'3'.'_default'];
$default4= $_POST['option'.$i.'4'.'_default'];
mysql_query("insert into subgames(game_id, input_id, statement, option1, option2, option3, option4, default1, default2, default3, default4) values ((SELECT id
FROM games WHERE game_name = '$game_name'), '$input_type', '$question', '$val1', '$val2', '$val3', '$val4', '$default1', '$default2', '$default3', '$default4')");
$error= mysql_error();
}
elseif($input_type=='2')
{
$question= $_POST['quest'];
$answer= $_POST['ans'];
// it is not implemented so leave it
}
}
Kindly help me i have tried a lot but i don't know where i am wrong. Thanks in advance
First of all I want to tell you that there are lots of problem in your code. Those I got are as follows.
1: There is no need to paste the entire html code of header and footer as well. This causes to skipped from the people who can give you answer, they run away after seeing lots of code. i.e. unnecessary.
2: You have defined multiple form tags, and these forms are nested to each other. every form should be closed before opening any other form tag.
3: Radio button tag's name should have the same for every group of the option, their value should be different not the name. for example for gender there should be two radio input tag with same name name="gender" and with different value like value="male" & value="female". you'll get the only one value for the radio button with same name.
4: if you are going to use session anywhere on the page, it first of all should be started before printing any output.
5: You have not given any value for the default value of radio button. So there should be a value attribute with different value inside that
6: whenever you are going to name a funtion, name it according to its functionality, not like a, b, c. Here I'm going to change your form to form_multiple() and form1 to form_input()
7: when you are going to choose input field for more than one question, then you'll have two input field with the same name, that is not allowed. So, let here also pass the i to the function.
====================
here is the solution for your code.
1: I have removed the numbers from default in radio button.
form.php
<?php
$i = $_POST['i'];
$_SESSION["input_type"][$i] = $_POST["type"];
if ($_SESSION["input_type"][$i] == 1) {
form_multiple($i);
} elseif ($_SESSION["input_type"][$i] == 2) {
form_input($i);
}
function form_multiple($i)
{
?>
<div class="control-group">
<div class="controls">
<textarea class="large m-wrap" placeholder=" Statement " cols="50" rows="3" name="statement<?php echo $i; ?>" style="text-align:center;" id="statement<?php echo $i; ?>"></textarea>
</div>
</div>
<div class="name">
<input name="option<?php echo $i . '1'; ?>" id="option<?php echo $i . '1'; ?>" placeholder="Option 1"
style="width:170px;" type="text"/>
<input name="option<?php echo $i . '2'; ?>" id="option<?php echo $i . '2'; ?>" type="text" style="width:170px;"
placeholder="Option 2"/>
<input name="option<?php echo $i . '3'; ?>" id="option<?php echo $i . '3'; ?>" type="text" style="width:170px;"
placeholder="Option 3"/>
<input name="option<?php echo $i . '4'; ?>" id="option<?php echo $i . '4'; ?>" type="text" style="width:170px;"
placeholder="Option 4"/>
</div>
<div class="control-group">
<div class="controls">
Choose Default Option
<br/>
<label class="radio">
<input type="radio" value="1" name="option<?php echo $i; ?>_default" id="option<?php echo $i . '1'; ?>_default"/>
Option 1
</label>
<br/>
<label class="radio">
<input type="radio" value="2" name="option<?php echo $i; ?>_default" id="option<?php echo $i . '2'; ?>_default"
checked />
Option 2
</label>
<br/>
<label class="radio">
<input type="radio" value="3" name="option<?php echo $i; ?>_default" id="option<?php echo $i . '3' ?>_default"/>
Option 3
</label>
<br/>
<label class="radio">
<input type="radio" value="4" name="option<?php echo $i; ?>_default" id="option<?php echo $i . '4'; ?>_default"/>
Option 4
</label>
</div>
</div>
<?php
}
//end
?>
<?php
function form_input($i)
{
?>
<div class="control-group" id="field" name="field">
<label class="control- label">Answer</label>
<div class="controls">
<input type="text" placeholder="Answer" id="ans" name="ans_<?php echo $i; ?>" class="m-wrap small"/>
</div>
</div><?php
}
?>
=====================================
In the below page I have removed some of the tags to shorten the answer
and I also have commented the form tags, so that you can analyze your errors. These two forms were inside another form tag
I don't know where you have defined $_SESSION["q_inc"] variable. I assume that this variable will have some integer value inside.
main content page
<?php
session_start();
include_once("../Includes/db_connection.php");
//include_once("form.php");
if(isset($_POST['total_q'])){
$_SESSION["q_inc"]=$_POST['total_q'];
}
if (!isset($_SESSION["q_inc"])) {
$_SESSION["q_inc"] = 2;
}
$_SESSION["questNo"] = $_SESSION["q_inc"];
if (!isset($_SESSION["lecturer_id"])) {
header("Location:../login.php");
}
//if($_SERVER['add_q'] == 'POST')
//$counter=0;
if (isset($_POST['add_q'])) {
$_SESSION["q_inc"]++;
}
?>
<!DOCTYPE html>
<!--[if IE 8]>
<html lang="en" class="ie8"> <![endif]-->
<!--[if IE 9]>
<html lang="en" class="ie9"> <![endif]-->
<!--[if !IE]><!-->
<html lang="en"> <!--<![endif]-->
<!-- BEGIN HEAD -->
<head>
<script type="text/javascript" src="../includes/jquery.js"></script>
<meta charset="utf-8"/>
<title>ClassEx</title>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<meta content="" name="description"/>
<meta content="" name="author"/>
<link href="../assets/bootstrap/css/bootstrap.min.css" rel="stylesheet"/>
<link href="../assets/css/metro.css" rel="stylesheet"/>
<link href="../assets/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet"/>
<link href="../assets/font-awesome/css/font-awesome.css" rel="stylesheet"/>
<link href="../assets/fullcalendar/fullcalendar/bootstrap-fullcalendar.css" rel="stylesheet"/>
<link href="../assets/css/style.css" rel="stylesheet"/>
<link href="../assets/css/style_responsive.css" rel="stylesheet"/>
<link href="../assets/css/style_default.css" rel="stylesheet" id="style_color"/>
<link rel="stylesheet" type="text/css" href="../assets/chosen-bootstrap/chosen/chosen.css"/>
<link rel="stylesheet" type="text/css" href="../assets/uniform/css/uniform.default.css"/>
<link rel="shortcut icon" href="../assets/img/favicon.ico"/>
</head>
<!-- END HEAD -->
<!-- BEGIN BODY -->
<body class="fixed-top">
<!-- BEGIN HEADER -->
<div class="header navbar navbar-inverse navbar-fixed-top">
<!-- BEGIN TOP NAVIGATION BAR -->
<div class="navbar-inner">
<div class="container-fluid">
<!-- BEGIN LOGO -->
<a class="brand" href="#">
<img src="../assets/img/logoclassex.jpg" alt="logo" height="35px" width="35px""/>
</a>
<!-- END LOGO -->
<!-- BEGIN RESPONSIVE MENU TOGGLER -->
<a href="javascript:;" class="btn-navbar collapsed" data-toggle="collapse" data-target=".nav-collapse">
<img src="../assets/img/menu-toggler.png" alt=""/>
</a>
<!-- END RESPONSIVE MENU TOGGLER -->
<!-- BEGIN TOP NAVIGATION MENU -->
<ul class="nav pull-right">
<!-- BEGIN NOTIFICATION DROPDOWN -->
<!-- END NOTIFICATION DROPDOWN -->
<!-- BEGIN INBOX DROPDOWN -->
<!-- END INBOX DROPDOWN -->
<!-- BEGIN TODO DROPDOWN -->
<!-- END TODO DROPDOWN -->
</ul>
<!-- END TOP NAVIGATION MENU -->
</div>
</div>
<!-- END TOP NAVIGATION BAR -->
</div>
<!-- END HEADER -->
<!-- BEGIN CONTAINER -->
<div class="page-container row-fluid">
<!-- BEGIN SIDEBAR -->
<div class="page-sidebar nav-collapse collapse">
<!-- BEGIN SIDEBAR MENU -->
<ul>
<li>
<!-- BEGIN SIDEBAR TOGGLER BUTTON -->
<div class="sidebar-toggler hidden-phone"></div>
<!-- BEGIN SIDEBAR TOGGLER BUTTON -->
</li>
<li class="start ">
<a href="lecturer.php">
<i class="icon-home"></i>
<span class="title">Dashboard</span>
</a>
</li>
<li class="">
<a href="../includes/logout.php">
<i class=" icon-off"></i>
<span class="title">Logout</span>
</a>
</li>
</ul>
<!-- END SIDEBAR MENU -->
</div>
<!-- END SIDEBAR -->
<!-- BEGIN PAGE -->
<div class="page-content">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
<input type="hidden" id="session" data="#Request.RequestContext.HttpContext.Session['questNo']" />
<!-- <a class="btn green" type="submit" ><i class="icon-plus" ></i></a> -->
Enter no of question<input name="total_q" type="text" />
<input type="submit" />
<!--<button type="submit" id="add_q" name="add_q" class="btn green"><i class="icon-plus"></i></button>-->
</form>
<form action="storeGame.php" method="POST">
<div class="control-group">
<label class="control-label">Game Name</label>
<div class="controls">
<input type="text" id="game_name" name="game_name" placeholder="Enter Game Name"
class="m-wrap large"/>
</div>
</div>
<!-- BEGIN BORDERED TABLE PORTLET-->
<?php
$q_no = 5;
for ($i = 0; $i < $_SESSION["q_inc"]; $i++) {
$temp = $i;
?>
<div class="portlet box yellow">
<div class="portlet-title">
<h4><i class="icon-coffee"></i>#<?php echo ($temp + 1); ?></h4>
<div class="tools">
</div>
</div>
<div class="portlet-body">
<table class="table table-bordered table-hover">
<div class="control-group">
<label class="control-label"> Add Input</label>
<div class="controls">
<select class="medium m-wrap question_type" data-question-no="<?php echo $i; ?>"
tabindex="1" id="type<?php echo $i; ?>" name="type<?php echo $i; ?>">
<option value="">Input Type</option>
<option value="1">Multiple Choice</option>
<option value="2">Input Field</option>
</select>
</div>
<div id="answer_no_<?php echo $i; ?>"></div>
</div>
</tbody>
</table>
</div>
</div>
<?php
}
?>
<script>
$(document).ready(function () {
$('.question_type').change(function () {
var question_no = $(this).attr('data-question-no');
$.ajax({
url: "form.php",
type: 'post',
data: {
type: $(this).val(),
i: question_no
},
success: function (data) {
$('#answer_no_' + question_no).html(data);
}
});
});
});
</script>
<!-- END BORDERED TABLE PORTLET-->
<!-- BEGIN PAGE CONTAINER-->
<div class="container-fluid">
<!-- BEGIN PAGE CONTENT-->
<div class="row-fluid">
<div class="span12">
<!-- <form action="-->
<?php //echo $_SERVER['PHP_SELF']; ?><!--" method="POST" >-->
<!-- <input type="hidden" id="session" data="#Request.RequestContext.HttpContext.Session['questNo']" />-->
<!-- <button type="submit" id="add_q" name="add_q" class="btn green"><i class="icon-plus"></i></button>-->
<!-- </form>-->
</div>
<!-- END PAGE CONTENT-->
</div>
<!-- END PAGE CONTAINER-->
<button type="submit" class="btn yellow btn-block" id="getGames" name="getGames" class="btn green">
Create Game <i &nbsp class="m-icon-big-swapright m-icon-white"></i></button>
</form>
</div>
</div>
<!-- END PAGE -->
</div>
<!-- END CONTAINER -->
<!-- BEGIN FOOTER -->
<div class="footer">
University of Passau ClassEx Team
<div class="span pull-right">
<span class="go-top"><i class="icon-angle-up"></i></span>
</div>
</div>
<!-- END FOOTER -->
<!-- BEGIN JAVASCRIPTS -->
<!-- Load javascripts at bottom, this will reduce page load time -->
<script src="../assets/js/jquery-1.8.3.min.js"></script>
<script src="../assets/breakpoints/breakpoints.js"></script>
<script src="../assets/jquery-slimscroll/jquery-ui-1.9.2.custom.min.js"></script>
<script src="../assets/bootstrap/js/bootstrap.min.js"></script>
<script src="../assets/js/jquery.blockui.js"></script>
<script src="../assets/js/jquery.cookie.js"></script>
<script src="../assets/fullcalendar/fullcalendar/fullcalendar.min.js"></script>
<script type="text/javascript" src="../assets/uniform/jquery.uniform.min.js"></script>
<script type="text/javascript" src="../assets/chosen-bootstrap/chosen/chosen.jquery.min.js"></script>
<!-- ie8 fixes -->
<!--[if lt IE 9]>
<script src="../assets/js/excanvas.js"></script>
<script src="../assets/js/respond.js"></script>
<![endif]-->
<script src="../assets/js/app.js"></script>
<script>
jQuery(document).ready(function () {
// initiate layout and plugins
App.setPage('calendar');
App.init();
});
</script>
<!-- END JAVASCRIPTS -->
</body>
<!-- END BODY -->
</html>
===============================
There should be only one default option field in the database. Here I have removed all those and added one named "default_option"
storeGame.php
<?php
session_start();
include_once("../Includes/db_connection.php");
$lecturer_id = $_SESSION["lecturer_id"];
$game_name=mysql_real_escape_string($_POST['game_name']);
echo $_SESSION["lecturer_id"];;
echo $game_name;
$sql="insert into games(game_name, lecturer_id) values ('$game_name', '$lecturer_id')";
if(!mysql_query($sql)){
echo "Error in storing into database!<br/>";
}
for($i=0;$i<$_SESSION["q_inc"];$i++)
{
$input_type = mysql_real_escape_string($_POST['type'.$i]);
if($input_type=='1')
{
$question= $_POST['statement'.$i];
$val1= $_POST['option'.$i.'1'];
$val2= $_POST['option'.$i.'2'];
$val3= $_POST['option'.$i.'3'];
$val4= $_POST['option'.$i.'4'];
//Here should be only one default value
$default_option= $_POST['option'.$i.'_default'];
/*$default1= $_POST['option'.$i.'1'.'_default'];
$default2= $_POST['option'.$i.'2'.'_default'];
$default3= $_POST['option'.$i.'3'.'_default'];
$default4= $_POST['option'.$i.'4'.'_default'];*/
$sql=" insert into subgames( game_id, input_id, statement, option1, option2, option3, option4, default_option)
values ( (SELECT id FROM games WHERE game_name = '$game_name' limit 1), '$input_type', '$question', '$val1', '$val2', '$val3', '$val4', '$default_option')";
if(!mysql_query($sql)){
echo "Error";
}
else{
echo "Success";
}
//$error= mysql_error();
}
elseif($input_type=='2')
{
$question= $_POST['quest'];
$answer= $_POST['ans'];
// it is not implemented so leave it
}
}

shows the div if the value of the input field matched

the hidden div will appear if the selected value from the form matches the value of the div
the script for detecting the value of the input field
<script> $(document).ready(function() {
$("input[name$='sel']").ready(function() {
var test = $(this).val();
$("div.desc").hide();
$("#sel" + test).show();
}); });
</script>
value of the field and the divs that are supposed to show when the value of the input field is correct
<table><tr><td> <strong><font size="3" >Payment Method</font></strong>
<p> <?php echo $_POST["sel"]; ?> <input type="radio" name="sel" value="<?php echo $_POST["sel"]; ?>" /> </p> <!====================================================================================> </td><td> <img src="img/sep.png" /> </td><td> <!====================================================================================> <div id="selCreditCard" class="desc"> <table><tr><td>
<p> <label for="card"><strong>Card Number : </font></strong></label><?php echo $_POST["card"]; ?>
<input name="card" type="hidden" id="card" value="<?php echo $_POST["card"]; ?>" style="width:85px;"
class="validate[custom[card]] text-input" /> </p> <br>
<label for="ccv" ><strong>CVV2 Code : </strong></label><?php echo $_POST["ccv"]; ?>
<input type="hidden" name="ccv" id="ccv" style="width:30px;margin-bottom:2px;margin-left:12px;" value="<?
php echo $_POST["ccv"]; ?>" />
<br>
<label><strong>Expiration Date</strong>
<p> <label for="mon"><strong>Month : </strong></label><?php echo $_POST["mon"]; ?>
<input type="hidden" name="mon" id="mon" style="width:15px;" value="<?php echo $_POST["mon"]; ?>" />
<label for="yir" ><strong>Year : </strong></label><?php echo $_POST["yir"]; ?>
<input type="hidden" name="yir" id="yir" style="width:15px;" value="<?php echo $_POST["yir"]; ?>" />
</p>
</div> <!====================================================================================> <div id="selPaypal" class="desc" style="display: none;margin-left:20px;margin-top:1px;margin-bottom:1px;"> Please make sure that you have paid the amount on the Paypal payment page.<br> If not click here to proceed <img src="img/ppal.png" height="36" style="margin-bottom:-13px;" onclick="window.open
('paypal/paypal.html','Paypal','width=450,height=300,left=160,top=170');" style="cursor:pointer;"/> </div> <!====================================================================================> <div id="selWireTransfer" class="desc" style="display: none;margin-left:20px;margin-top:0px;margin-bottom:-5px;"> <font size="0.5"> <strong>Processing Time</strong><br> A bank wire is not an instant transfer. "Two to three business days" are<br> required to process the transfer and
allow the recipient to see the wired funds.</font> </div> <!====================================================================================> </td></tr></table>
i cant get the divs to show on the next page even if the correct value was entered on the field what approach will be much efficient or do you think will work for this code?
You should have used click event on input.
<script>
$(document).ready(function() {
var test = $("input[name='sel']").val();
$("div.desc").hide();
$("#sel" + test).show();
});
</script>

Categories

Resources