I am new to stackoverflow and little bit confused about what to title this question, but let me explain what my problem is.
I am developing survey system where I have option for admin to enter the questions and answers from the back end, so in the front end I am displaying the question, answers using the while loop in the form.
My problem is how to write the code to enter the multiple values (survey filled by the customer) in the mysql. I am attaching my code here.
$(function() {
$('form').bind('submit', function(){
$.ajax({
type: 'post',
url: "/prs/pageCode.php",
data: $("form").serialize(),
success: function() {
alert("Data Saved, Press Next!");
}
});
return false;
});
});
form is below
<form name="form" id="form" class="form-horizontal" action="#"
method="POST">
<fieldset>
<div class="control-group">
<label class="control-label" for="focusedInput">Name</label>
<div class="controls">
<input name="pname" class="input-xlarge focused" id="focusedInput"
type="text" value="" required>
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">Email</label>
<div class="controls">
<input name="pemail" class="input-xlarge focused" id="focusedInput"
type="text" value="" required>
</div>
</div>
<div class="control-group">
<label>We would like to post your comments to internet rating sites,
while we may use your name or an alias name. Please select your
desire:</label>
</div>
<div class="control-group">
<label class="control-label" for="focusedInput"></label>
<div class="controls">
<p>
<label> <input type="radio" name="choice" value="yes" id="choice_0">
Yes, use my name as entered above
</label> <label> <input type="radio" name="choice" value="no"
id="choice_1" onChange="enabletextbox();"> Use the alias name, as
I enter below
</label> <input id="alias" name="alias" type="text"
class="input-xlarge focused">
</p>
</div>
</div>
</fieldset>
</div>
<div class="tab-pane" id="tab2">
<fieldset>
<?php
$i=1;
while($row=mysqli_fetch_array($questions)){
?>
<div class="control-group">
<label class="control-label" for="focusedInput">(<?php echo $i;?>) <?php
$questionid = $row['question_id'];
$question = $row['question'];
echo $row['question']; ?></label>
<div class="controls">
<?php
if($row['answer_type']=="Ratings") {
echo "
<p>
Low<input type='radio' name='rating$i' value='1' id='rating_0'>
<input type='radio' name='rating$i' value='2' id='rating_1'>
<input type='radio' name='rating$i' value='3' id='rating_2'>
<input type='radio' name='rating$i' value='4' id='rating_3'>
<input type='radio' name='rating$i' value='5' id='rating_4'>High
</p>
";
}
else if ($row['answer_type']=="Comments") {
echo "<textarea name='answer' cols='' rows=''></textarea>";
}
$i++;
echo "<br />";
$query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_rating,answer_freeresponse) values (1,$surveyid,$questionid,'$question','$rating$i','$_POST[answer]')";
$result2 = mysqli_query($con,$query2);
if(!$result2) {
echo mysqli_error($result2);
}
?>
</div>
</div>
<?php }?>
</fieldset>
</div>
<div class="tab-pane" id="tab3">
<fieldset>
<div class="control-group">
<label class="control-label" for="focusedInput">Testimonial about
your care by <?php echo $_SESSION['doctorname'];?>
</label>
<div class="controls">
<textarea name="review" cols="5" rows="5" required></textarea>
</div>
</div>
<div class="form-actions">
<input type="submit" class="btn btn-primary"
value="Save & Press Finish"></input>
</div>
</fieldset>
pageCode.php
<?php
session_start();
require_once('config.php');
$con=mysqli_connect(HOST,USER,PASSWORD,DATABASE);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$alias = $_POST['choice'];
if($alias=="yes")
$aliasname = $_POST['pname'];
else
$aliasname = $_POST['alias'];
$sql="INSERT INTO reviews (doctor_id,doctor_name,survey_id,name,email,display_name,overall_review)
VALUES
('$_SESSION[doctorid]','$_SESSION[doctorname]',$_SESSION[surveyid],'$_POST[pname]','$_POST[pemail]','$aliasname','$_POST[review]')";
$result = mysqli_query($con,$sql);
//$result2 = mysqli_query($con,$_SESSION['insertallquery']);
if(!$result) {
echo mysqli_error($result);
}
?>
I actually want to insert the answers into database filled by customers.
You need to store answers in the review table or a separate table its your wish.
but in general, what I understood you have a separate table for questions with the question ids.
So, you need to add column in the review table or a separate table with the question ids as the field names.
and store the respective answers in those column for that user.
For eg. considering adding column in existing reviews table :
INSERT INTO reviews (doctor_id,doctor_name,survey_id,name,email,display_name,overall_review,q1,q2,q3...)
so on according to number of questions you have.
Also, when you add a new question to the questions table you will have to make sure to add a column in this table using ALTER TABLE with the.
For getting the answers as the question are in a while loop, create a array.
and insert those values into mysql using a while loop iterating the array.
The query inside the while loop will be like this :
INSERT INTO reviews (q.$i) VALUES ($answer[$i]) WHERE docter_id='$_SESSION[doctorid]'
You could get the values from IDs/class names or tag names itself then you could store it in an array then you can pass it to your jquery code( $.ajax ) or ($.post) then accept the array variable declared in your javascript
but our tags must contain temp index values example.
Another way is to pass the answers as JSONs then you could use json_encode in php to access the answers
Related
I have a page which is showing around 3000 entries. what i am trying to do is when i click on filter button the data of only that entry will change. but currently, when i click on filter button the data of all the entries are changing.
here is my code:-
<?php
include("config.php");
$sql="SELECT * FROM inventory_details where status ='0' limit 0,100";
$query=mysqli_query($conn, $sql);
while($row=mysqli_fetch_assoc($query)){
$id = $row['id'];
$inventorybackground = $row['inventorybackground'];
$inventorycolor = $row['inventorycolor'];
$firm_name = $row['firm_name'];
$position = $row['position'];
$city = $row['city'];
$catagory_name = $row['catagory_name'];
$supplier = $row['supplier'];
if (isset($_POST['update'])){
$position=$_POST['position'];
$update_query="UPDATE inventory_details SET position ='$position' WHERE id='$id'";
$run_update=mysqli_query($conn, $update_query);
}
?>
<div class="container">
<div class="row" style="background-color: <?php echo $inventorybackground; ?> !important; color: <?php echo $inventorycolor; ?>; padding-top: 10px;">
<form class="form-horizontal" method="post" enctype="multipart/form-data">
<div class="col-md-1 inventory-data">
<input type="text" class="form-control" id="id" name="id" value="<?php echo $id;?>" disabled>
</div>
<div class="col-md-3 inventory-data">
<input type="text" class="form-control" id="firm_name" name="firm_name" value="<?php echo $firm_name;?>" disabled>
</div>
<div class="col-md-1 inventory-data">
<input type="text" class="form-control" id="position" name="position" value="<?php echo $position;?>">
</div>
<div class="col-md-1 inventory-data">
<input type="text" class="form-control" id="city" name="city" value="<?php echo $city;?>" disabled>
</div>
<div class="col-md-2 inventory-data">
<input type="text" class="form-control" id="catagory_name" name="catagory_name" value="<?php echo $catagory_name;?>" disabled>
</div>
<div class="col-md-2 inventory-data">
<input type="text" class="form-control" id="supplier" name="supplier" value="<?php echo $supplier;?>" disabled>
</div>
<div class="col-md-2 inventory-data">
<button type="submit" name="update" value="update" class="btn btn-primary">Filter</button>
</div>
</form>
</div>
</div>
<?php } ?>
Currently, i am only trying to change the position and I think, this problem is because of loop.
Please help me out, Thanks in advance.
When you click the button, the entire list is updated on your loop to list the rows.
Add this code at your page to detect the form POST to do the update:
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// perform update here
$id = $_POST['id'];
$position = $_POST['position'];
$update_query="UPDATE inventory_details SET position ='$position' WHERE id='$id'";
$run_update=mysqli_query($conn, $update_query);
}
Edit:
Your input can't be disabled, try to use input hidden to pass id to POST:
<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
If you need to show the id on input text, change the name to different name istead of id, because it will be used from input hidden
while($row=mysqli_fetch_assoc($query)){
This has ending bracket on the end. So you are basically looping through all the data and updating it all. Do the update query outside of loop.
$update_query="UPDATE inventory_details SET position ='$position' WHERE id='$id'";
You have SQL injection vulnerability here, please use prepared statements.
https://www.w3schools.com/php/php_mysql_prepared_statements.asp
If id is integer, you dont have to put quotation marks around it
I'm stumped. I've looked this up on multiple answers on Stackoverflow, but just can't get it. Maybe I'm just not seeing something.
I'm making a Family Feud game and using PHP and MySQL databases to store and retrieve information. For the Fast Money Round, I have a database with a Table called "FastMoney1" I'm using an HTML5 form and PHP to post the data in the form to that table, which has two columns: answer and score
I'm running my query through a for loop, but it's not posting anything to the table. I'm wondering what I'm doing wrong.
HTML:
<form method="post" class="form-horizontal">
<div class="form-group">
<div class="col-xs-9">
<input type="text" class="form-control" id="question1answer" name="answer[0]" placeholder="Question 1">
</div>
<div class="col-xs-3">
<input type="number" class="form-control" id="question1score" name="score[0]" placeholder="0">
</div>
</div>
<div class="form-group">
<div class="col-xs-9">
<input type="text" class="form-control" id="question1answer" name="answer[1]" placeholder="Question 2">
</div>
<div class="col-xs-3">
<input type="number" class="form-control" id="question1score" name="score[1]" placeholder="0">
</div>
</div>
<div class="col-xs-4 col-xs-offset-4" align="center">
<input type="submit" class="btn btn-success" name="Submit" />
</div>
</form>
PHP:
<?php
if(isset($_POST['submit'])){
require "config.php";
for ($i = 0; $i<count($_POST); $i++){
$answer = $_POST['answer'][$i];
$score = $_POST['score'][$i];
$sql = "INSERT INTO `fastMoney1`(`answer`, `score`) VALUES ('$answer','$score')";
if ($conn->query($sql) === TRUE) {
echo "";
} else {
echo $conn->error;
}
}
$conn->close();
echo "<meta http-equiv='refresh' content='0'>";
}
?>
All of this lives on the same PHP page, which is why I do not have an action attached to the form.
The config.php is an include that calls the host, username, password and database and opens the connection
Remember that PHP variables as case sensitive you have given name Submit in form while in php you are checking if(isset($_POST['submit'])){ which never become true.
change it to
if(isset($_POST['Submit'])){ //<----- S in upper case
EDIT
You also need to change your loop to
for ($i = 0; $i<count($_POST['answer']); $i++){
see your sql statement you don't need those ampersand in table name and column names INSERT INTO fastMoney1(answer, score) VALUES ('$answer','$score')
<input type="submit" class="btn btn-success" name="submit" />
i changed
name="Submit"
to
name="submit"
"Submit" to "submit" ----->"S" to "s"
and it works fine
i'm working on permissions in php,my requirement is i have list of menus..based on user role i have to give access to them.so a single user have access to multiple menus..my idea is i will select check boxes and store it in single database columns.and if i check any check box the value should be 'y' in database column field.and if not check means value should be 'x' in database column field(Example:y,x,x,y) i have a table like this following..
i'm working on permissions in php,my requirement is i have list of menus..based on user role i have to give access to them.so a single user have access to multiple menus..my idea is i will select check boxes and store it in single database columns.and if i check any check box the value should be 'y' in database column field.and if not check means value should be 'x' in database column field(Example:y,x,x,y) i have a table like this following..
role id,role name,permissions,description...
my code:
<form class="form-horizontal tasi-form" method="POST" name="register-form" id="register-form">
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Roles Title</label>
<div class="col-sm-10">
<input type="text" class="form-control round-input" name="roletitle" id="roletitle">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Permission</label>
<div class="col-sm-10">
<input type="checkbox" name="sb[]" id="checkbox_php" value="EMPLOYEES"/><label for="checkbox_php">EMPLOYEES</label>
<br/>
<input type="checkbox" name="sb[]" id="checkbox_asp" value="UNIT"/><label for="checkbox_asp">UNIT</label><br/>
<input type="checkbox" name="sb[]" id="checkbox_asp" value="SERVICES"/><label for="checkbox_asp">SERVICES</label><br/>
<input type="checkbox" name="sb[]" id="checkbox_asp" value="Appointment"/><label for="checkbox_asp">Appointment</label><br/>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 col-sm-2 control-label">Roles Description</label>
<div class="col-lg-10">
<textarea name="description" id="editor1" class="form-control" cols="30" rows="10"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 col-sm-2 control-label"></label>
<div class="col-lg-10">
<input type="hidden" value="roles" name="requesttype"/>
<button type="submit" class="btn btn-success">Save</button>
</div>
</div>
</form>
php code:
<?php
include("../config.php");
if(isset($_POST['requesttype'])&&$_POST['requesttype'] == 'roles'){
$insertInfo=array();
$insertInfo['ROLES_TITLE'] = $_REQUEST['roletitle'];
$insertInfo['PERMISSIONS'] = $_REQUEST['permission'];
$insertInfo['ROLES_DESCRIPTION'] = $_REQUEST['description'];
$date=date('Y-m-d H:i:s');
$insertInfo['CREATED_ON']=$date;
$insertinfo['UPDATED_BY']='';
$insertInfo['UPDATED_ON']=$date;
$res=$db->insert('ROLES', $insertInfo);
if($result)
{?>
//header("location:city");
<script>
window.location.href='rolesa';
</script>
<?php }
} ?>
I will suggest a alternative way, If you can store real value in db.
Like this,
dbCheckbox = "";
if(isset($_POST['sb'])){
$dbCheckbox = implode(',',$_POST['sb']);
}
When you retrieve data you can,
$dbCheckboxExplode = array();
if(!empty($checkDBValueOnThatCol)){
$dbCheckboxExplode = explode(",", checkDBValueOnThatCol);
}
And You better to keep checkbox value on display page as array like this,
<?php
$checkboxAllValues = array('EMPLOYEES', 'UNIT');
foreach($checkboxAllValues as $value){
?>
<input type="checkbox" name="sb[]" id="checkbox_asp" value="<?php echo $value; ?>"/><label for="checkbox_asp"><?php echo $value; ?></label><br/>
<?php
}
?>
When db data retrieve page,
<?php
$checkboxAllValues = array('EMPLOYEES', 'UNIT');
foreach($checkboxAllValues as $value){
$checked = "";
if(in_array($value, $dbCheckboxExplode)){
$checked = ' checked="checked"';
}
?>
<input <?php echo $checked; ?> type="checkbox" name="sb[]" id="checkbox_asp" value="<?php echo $value; ?>"/><label for="checkbox_asp"><?php echo $value; ?></label><br/>
<?php
}
?>
You can do this way:
<?php
$allValues = array('EMPLOYEES', 'UNIT');
$dbCheckbox = "";
if(isset($_POST['sb'])){
foreach($_POST['sb'] as $value){
if(in_array($value, $allValues)){
$dbCheckbox .= 'Y';
}else{
$dbCheckbox .= 'X';
}
$dbCheckbox .= ',';
}
}
?>
Create the table and with column which has datatype as varchar.
Give values to each check boxes and add the checked checkboxes values in that column
<input type="checkbox" name="games[]" value="1" <?php if(in_array(1,$my_stored_game)){echo "checked";}?>><label>Football</label><br>
<input type="checkbox" name="games[]" value="2" <?php if(in_array(2,$my_stored_game)){echo "checked";}?>><label>Basket Ball</label><br>
<input type="checkbox" name="games[]" value="3" <?php if(in_array(3,$my_stored_game)){echo "checked";}?>><label>Pool</label><br>
Add comma separated values in database column
check the complete answer here
PHP : insert multiple check boxes values into one MySQL cloumn
I uses this from to submit some information to '2co' as a payment method some of this information I need to validate it by PHP server-side and in the same time send a notification to the admin by mail to tell him that someone has been paid.
now this is the form.
<form method="post" action="https://sandbox.2checkout.com/checkout/purchase">
<div class="body">
<div class="half f_right">
<label for="card_holder_name" class="width_100per"><span class="fontRed2">*</span>card_holder_name
</label>
<input type="text" name="card_holder_name" id="card_holder_name"
class="form_textarea_rec"/>
</div>
<div class="half">
<label for="country" class="width_100per"><span class="fontRed2">*</span>country</label>
<select name="country" id="country" class="form_select_rec">
<option value="EGY" title="Egypt">Egypt</option>
<option value="SaudiArabia" title="saudi Arabia">Any other</option>
</select>
</div>
<div class="half">
<label for="street_address" class="width_100per"><span class="fontRed2">* </span>street_address </label>
<textarea name="street_address" id="street_address"
class="form_textarea_rec height_50px"></textarea>
</div>
<div class="half">
<label for="street_address2" class="width_100per"> address2</label>
<textarea name="street_address2" id="street_address2"
class="form_textarea_rec height_50px"></textarea>
</div>
<div class="half f_right">
<label for="city" class="width_100per"><span class="fontRed2">* city</span>
</label>
<input id="city" type="text" name="city" class="form_textarea_rec"/>
</div>
<div class="half f_right">
<label for="state" class="width_100per"><span class="fontRed2">*</span>
States</label>
<input id="state" type="text" name="state" class="form_textarea_rec"/>
</div>
<div class="half">
<label for="email" class="width_100per"> <span class="fontRed2">*</span>Email</label>
<input id="email" type="text" name="email" class="form_textarea_rec"/>
</div>
<div class="half">
<label for="zip" class="width_100per"> <span class="fontRed2">*</span>Zip</label>
<input id="zip" type="text" name="zip" class="form_textarea_rec"/>
</div>
<div class="half">
<label for="phone" class="width_100per"> <span class="fontRed2">*</span>
Phone</label>
<input id="phone" type="text" name="phone" class="form_textarea_rec"/>
</div>
</div>
<?php
$fullName = #$_POST['card_holder_name'];
?>
<input type='hidden' name='sid' value='*****'/>
<input type='hidden' name='mode' value='2CO'/>
<input type='hidden' name='li_0_type' value='product'/>
<input type='hidden' name='li_0_name' value='<?php echo $planName ?> '/>
<input type='hidden' name='li_0_price' value='<?php echo $planPrice ?> '/>
<input type='hidden' name='card_holder_name' value='<?php echo $fullName ?>'/>
<input type='hidden' name='street_address' value='<?php #$_POST['street_address']; ?>'/>
<input type='hidden' name='street_address2' value='<?php #$_POST['street_address2']; ?>'/>
<input type='hidden' name='city' value='<?php #$_POST['city']; ?>'/>
<input type='hidden' name='state' value='<?php #$_POST['state']; ?>'/>
<input type='hidden' name='zip' value='<?php #$_POST['zip']; ?>'/>
<input type='hidden' name='country' value='<?php #$_POST['country']; ?>'/>
<input type='hidden' name='email' value='<?php #$_POST['email']; ?>'/>
<div class="footer">
<input name="submit" type="submit" value="Continue" class="s_btn"/>
</div>
</form>
now I need to validate this fields and send the form and I need to send notes to the admin
I know about make it like this
<?php
if(isset($_POST['submit'])){
//remove the action from the form
//write the validation here ...
//and then send the mail
//but how to post all this in the end to '2co'??
}
?>
You could use curl to finally post it to 2co http://php.net/manual/en/book.curl.php
Sending mail is as simple as using mail function http://php.net/manual/en/function.mail.php
<?php
if(isset($_POST['submit'])){
//Do validation
//send mail
//post to 2co via curl
}
?>
As said in this question there are non curl ways to do so How do I send a POST request with PHP?
you are asking the wrong question, you should have asked "how to post data to another server using php?"
there are many answers here are some :
How do I send a POST request with PHP?
http://www.lornajane.net/posts/2010/three-ways-to-make-a-post-request-from-php
You need to do it by JavaScript on page load event.
Changes you need to do:
change action to same php page OR different AND check submit action by if(isset($_POST['submit'])) as you said.
validate all data, send mail, set php var $validated=true else false AND set some session variable which you should validate to confirm, on response from payment server. Ex: payment_id, user_id | these should also be sent to payment gateway and gateway returns on response, in your case I think sid
if($validated) echo the JavaScript code to create a hidden form with all information in hidden input field and submit form automatically on page load event.
on response from payment server, which will come on your specific php page validate session/user info.
If you are using jquery the autosubmit code will be like following:
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- if you are not using jQuery and want to then use above code -->
<?php if($validated) { ?> <script type="text/javascript">
$(function(){
var form = $('<form></form>');
form.attr("method", "post");
form.attr("action", link);
form.attr("style","display: none");
var field = $('<input />');
field.attr("type", "hidden");
field.attr("name", "post_var1");
field.attr("value", "val_1");
form.append(field); //echo values by php <?php echo $value1; ?> like:
var field1 = $('<input />');
field1.attr("type", "hidden");
field1.attr("name", "li_0_name");
field1.attr("value", "<?php echo $planName ?>");
form.append(field1);
//do same for all fields
$(document.body).append(form);
form.submit();
}
</script> <?php } ?>
For non jQuery (Pure JavaScript) code you can search OR ask me i'll edit the answer.
how can i get multiple value data from multi checkbox options? Following are my checkbox html code that can select multiple values
<li class='voteable-attribute off clearfix'>
<label class='primary formField'>Parking:
</label>
<div class='inputFields ieSucks'>
<div class='thereisonlybool'>
<label for='av_box:BusinessParking:valet'>
<input type='checkbox' id='av_box:BusinessParking:valet' name='BusinessParking_valet' value='1'>
Valet
</label>
</div>
<div class='thereisonlybool'>
<label for='av_box:BusinessParking:garage'>
<input type='checkbox' id='av_box:BusinessParking:garage' name='BusinessParking_garage' value='1'>
Garage
</label>
</div>
<div class='thereisonlybool'>
<label for='av_box:BusinessParking:street'>
<input type='checkbox' id='av_box:BusinessParking:street' name='BusinessParking_street' value='1'>
Street
</label>
</div>
<div class='thereisonlybool'>
<label for='av_box:BusinessParking:lot'>
<input type='checkbox' id='av_box:BusinessParking:lot' name='BusinessParking_lot' value='1'>
Private Lot
</label>
</div>
<div class='thereisonlybool'>
<label for='av_box:BusinessParking:validated'>
<input type='checkbox' id='av_box:BusinessParking:validated' name='BusinessParking_validated' value='1'>
Validated
</label>
</div>
</div>
</li>
From zend controller i can access single value as follow:
$data1 = $this->_getParam('input_name')
But in this case there is multiple value, how can i get this multiple values in zend controller and store it on array and save on db.Thanks
You have to create a element with this name BusinessParking[].
meand create this checkbox like array.
now when you get value controller then you get what you want.
Create the html page as:
<input type="hidden" name="id[]" value="<?php echo $value[i] ?>"/>
Note the name="id[]", that's the trick. Now in Zend you can get the values as:
$array = $this->getRequest()->getParam("id");