I have an HTML form that contains a number of checkboxes. When the user clicks the submit button the checked array is posted and a query runs based on that result.
I have a button allowing the user to save their result, when they insert their saved result into another table.
But this means I need to post the form result again which it doesn't allow me to do. How would I fix this?
PHP Code
<?php
if (isset($_POST['submit']) && isset($_POST['sport'])) {
$class = $_POST["sport"];
foreach ($class As $key => $value) {
$query = "SELECT *
FROM sport b
join sport a
on a.Tag = b.Name
where a.SportID<> b.SportID and a.Tag = '$value'";
$result = mysqli_query($con, $query) or die("Invalid Query");
while ($row = mysqli_fetch_assoc($result)) {
echo "* $row[Name]\n";
}
}
} else if (isset($_POST['saved'])) {
$arr_class = $_POST["sport"];
foreach ($arr_class As $key => $newvalue) {
$query2 = "INSERT INTO save (Username, Name)
SELECT '$username', b.Name
FROM sport b
JOIN sport a
on a.Tag = b.Name
where a.SportID <> b.SportID
and a.Name = '$newvalue'";
$result2 = mysqli_query($con, $query2) or die('Result could not be saved!');
}
echo 'Result saved!';
}
JS Code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.submit-button').click(function (ev) {
var $item = $(this);
var formData = $('form').serialize();
$.ajax({
data: formData+'&submit=submit',
url:'test1.php',
type: "POST",
dataType: "html",
success:function (data) {
console.log(data);
if(confirm('You want to save \n'+data+ ' as your sport')){
$.ajax({
data: formData+'&saved=saved',
url:'test1.php',
type: "POST",
dataType: "html",
success:function (data) {
console.log(data);
}
});
}
}
});
});
});
</script>
HTML Code
<form id="form" action="" method="post">
<input type="checkbox" name="sport[]" value="Football">Football<br>
<input type="checkbox" name="sport[]" value="Rugby">Rugby<br>
<input type="checkbox" name="sport[]" value="Golf">Golf<br>
<input type="checkbox" name="sport[]" value="Basketball">Basketball<br>
<br> <input type="button" class="submit-button btn btn-info" name="submit" value="submit">
<input type="submit" style="display:none;">
</form>
Related
Okay so i need the following to be done.
There is an input form, with 2 input fields and a submit button.
Once the values are entered, the program does a calculation and displays the sum.
The calculation (sum) then gets stored into a database, then using Ajax gets filled dynamically in another form input fields.
Is there a way that once the submit button is clicked, it automatically does it 5 times with a loop counter?
AJAX
$(document).ready(function(){
var data_amount = $('#value').val();
$.when(
$.ajax({ // First Request
url:"fetch.php",
method:"POST",
data:{value:data_amount},
dataType:"text",
contentType: false,
processData:false,
success:function(data)
{
$('#load_data').val(data);
}
}),
$.ajax({ //Seconds Request
url:"fetch_another.php",
method:"POST",
data:{value_2:data_amount},
dataType:"text",
contentType: false,
processData:false,
success:function(data)
{
$('#load_data_2').val(data);
}
})
)
});
FORM
<form method="post" action="test.php">
<input type="number" name="value" id="load_data"></input>
<input type="number" name="value_2" id="load_data_2"></input>
<button type="submit" name="get_values"></input>
</form>
PHP
<?php
if(isset($_POST['value'])) {
$value = $_POST['value'];
}else{
echo "Nothing Entered";
}
?>
<?php
if(isset($_POST['value_2'])) {
$value_2 = $_POST['value_2'];
}else{
echo "Nothing Entered";
}
?>
$result = $value * $value_2;
$result_2 = $value * $value_2 * 2000;
$sql = "INSERT INTO values(value) VALUES('".$result."')";
mysqli_query($con,$sql);
$sql = "INSERT INTO values_2(value_2) VALUES('".$result_2."')";
mysqli_query($con,$sql);
FORM TO LOOP
<form method="post" action="test.php">
<input type="number" name="value" id="load_data" hidden=""></input>
<input type="number" name="value_2" id="load_data_2" hidden""></input>
<button id="somebutton" type="submit" name="get_values"></input>
</form>
I am pretty sure that a for loop with a counter could do the trick, i am just not sure how. Also i know this can be done without the form, just for this particular case i need it to be achieved with the forms.
I am trying something like this, which is not working out:
<script>
$(document).ready(function() {
$("#somebutton").click(function () {
for (var counter = 0; counter < 5; counter++) {
$("#somebutton").click();
}
});
});
</script>
I was making like button with php and ajax/jquery and it wont be reload when you click it will just reload the div but it reload the div but the number of like don't add
here my code.
<div id="Post-ImVi-Action">
<script>
$(document).ready(function () {
$('#Post-ImVi-Action-Like').click(function (e) {
e.preventDefault();
var name = $('#Like_Poster_name').val();
var email = $('#Like_Post_1').val();
$.ajax
({
type: "POST",
url: "php-SocialMedia-Posting-Like.php",
data: { "Poster_name": name, "Post_1": email },
success: function (data) {
$("#Post-ImVi-Action-Like").html(data);
}
});
});
});
</script>
<input type="Hidden" name="Poster_Name" id="Like_Poster_name" value="<?php echo $Post_Row["Poster_Name"];?>">
<input type="Hidden" name="Post_1" id="Like_Post_1" value="<?php echo $Post_Row["Post_1"];?>">
<input type="Hidden" name="ID" id="Like" value="<?php echo $Post_Row["ID"];?>">
<button id="Post-ImVi-Action-View">158K</button>
<button id="Post-ImVi-Action-Like" type="button" name="Like_Button" >
<?php
$Post = $Post_Row["Post_1"];
$Sql_like_count = "SELECT * FROM `likes` WHERE post = '$Post'";
$Result_like_Count = mysqli_query($conn, $Sql_like_count);
while ($ROW_LIKE = mysqli_fetch_array($Result_like_Count)) {
echo $ROW_LIKE['Likes'];
}
?>
</button>
<button id="Post-ImVi-Action-Share">815</button>
<div id="Post-ImVi-Action-Face"></div>
</div>
Here is php-SocialMedia-Posting-Like.php
<?php
include"php-MAIN-Info.php";
include"php-MAIN-SignUp+Database.php";
$Post = $_POST['Post_1'];
$Poster_Name = $_POST['Poster_Name'];
$Sql = "SELECT* FROM Likes WHERE Post = '$Post' AND Poster_Name = '$Poster_Name'";
$Like_Result = mysqli_query($conn, $Sql);
while ($Like_Row = mysqli_fetch_array($Like_Result)) {
$Like_Add = $Like_Row['Likes'] + '1';
$Sql_Like_Add = "UPDATE Likes SET Likes = '$Like_Add' WHERE Post = '$Post' AND Poster_Name = '$Poster_Name';";
$Like_Result_Add = mysqli_query($conn, $Sql_Like_Add);
if ($Like_Result_Add) {
echo $Like_Add;
}
}
update:
i found a error
php-Home-MSOHome.php:352 Uncaught ReferenceError: data is not defined
please try to help cause i'm new to this web.Thanks
why is no one answering my question you k i'm still looking for the answers
I did it its a php error udfinde Poster_name its Poster_Name
I have a checkbox:
<input value="".$count_FRID."" name="rubric_chkbox[]" />
I'm using jquery and ajax to get the data and use foreach loop to insert it to my database as follows:
var rubricChkbox = new Array();
$("input:checked").each(function() {
rubricChkbox['rubric_chkbox[]'].push($(this).val());
});
console.log(rubricChkbox);
$.ajax({
url: "Queries/save.php",
type: "POST",
data: {
"rubricChkbox":rubricChkbox
},
success: function(yey){
console.log(yey);
alert(yey);
}
});
For deleting of checkbox:
<input value="1" name="rubric_chkbox[]" type="checkbox" class="checkbox" />
<input value="2" name="rubric_chkbox[]" type="checkbox" class="checkbox" />
jQuery and AJAX:
var rubricChkbox = new Array();
var uncheked = new Array();
$(".checkbox:checked").each(function() {
rubricChkbox.push($(this).val());
});
$(".checkbox:checkbox:not(:checked)").each(function() {
uncheked.push($(this).val());
});
$.ajax({
url: "Queries/save.php",
type: "POST",
data: {
"rubricChkbox":rubricChkbox
},
success: function(yey){
console.log(yey);
alert(yey);
}
});
For example if 1 and 2 values are inserted in my database, and if I unchecked checkbox 1 and 2, shouldn't the code above work for deleting rows?
I'm getting below error:
Uncaught TypeError: Cannot read property 'push'
EDIT, save.php:
if (isset($_POST['rubricChkbox']) || isset($_POST['uncheked']) || isset($_POST['user_id'])) {
$rubric_value = $_POST['rubricChkbox'];
$rubric_uncheck_value = $_POST['uncheked'];
$IDuser = $_POST['user_id'];
foreach($rubric_value as $rubric_check) {
$sql_check = "SELECT raw_selected_rubric FROM rubric_selected INNER JOIN cmat_composition ON rubric_selected.ID_cmat = cmat_composition.ID_cmat WHERE rubric_selected.ID_users = '$IDuser' AND raw_selected_rubric = '$rubric_check'
AND rubric_selected.Saved = '1'";
$result_check = mysqli_query($conn,$sql_check);
if (mysqli_num_rows($result_check) <= 0){
$sql_raw = "INSERT INTO rubric_selected (raw_selected_rubric, Saved, ID_users)
VALUES ('$rubric_check', '1', '$IDuser')";
mysqli_query($conn, $sql_raw);
}
}
}
You don't need to put the brackets [ ] in your array variable. Use rubricChkbox.push($(this).val());
var rubricChkbox = new Array();
$("input:checked").each(function() {
if($(this).attr("id") == "option1"){
// if the id of checkbox is option1 then push it
rubricChkbox.push($(this).val());
}
});
console.log(rubricChkbox);
$.ajax({
url: "Queries/save.php",
type: "POST",
data: {"rubricChkbox":rubricChkbox
},
success: function(yey){
console.log(yey);
alert(yey);
}
});
Edit: For your additional question, yes you can add a class specific for your checkboxes and loop through them instead;
Run the code below.
$("button").click(function() {
var unchecked = new Array();
$(".checkbox:not(:checked)").each(function() {
unchecked.push($(this).val());
});
console.log(unchecked);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input value="1" class="checkbox" type="checkbox" name="rubric_chkbox[]" />
<input value="2" class="checkbox" type="checkbox" name="rubric_chkbox[]" />
<button>Validate</button>
What I'm trying to do is make a search page where the result would be on the same page. My concept is to first show all data and then there will be one textbox and one submit button. Below that, I will show the result which is paginated. How do I achieve that?
<input type ="text" placeholder = "search" name = "search">
<input type ="submit" name = "submit">
<?php
if(isset($_POST['submit']))
$search = $_POST['search'];
{
$query = "SELECT * FROM table WHERE search = '$search' ";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
//echo my result here.
}
}
Take a look at jquery and ajax. I did something similar as a test awhile back:
var request = $.ajax({
type: "GET",
url: url,
data: $(".form-wrapper").serialize()
});
request.done(function(data) {
if (data.search("No results") == -1) {
$("#result").show();
$("#result").html(data);
} else {
$('#result').html('').hide();
}
});
request.fail(function(jqXHR, msg) {
alert(msg);
});
<form class="form-wrapper cf">
<input type="text" id="text" name="q" placeholder="" required>
<button type="submit" disabled>Search</button>
</form>
<div id="result"></div>
I have this form in a foreach loop, so it shows multiple times on the same page.
Everything submits fine in each form EXCEPT the radio buttons and checkboxes. They don't save the values to the db.
EDIT: I've narrowed it down to the ajax causing the error but can't figure out how to correct it.
<form action="process.php" method="post" name="editInvoice'.$invoice_id.'" id="editInvoiceForm'.$invoice_id.'" class="editInvoiceForm edit_invoice_container" enctype="multipart/form-data">
<div class="form_item_row">
<input type="radio" value="Unsent" '.$unsent.' name="status"/><span class="choice">Unsent</span>
<input type="radio" value="Sent" '.$sent.' name="status"/><span class="choice">Sent</span>
<input type="radio" value="Paid" '.$paid.' name="status"/><span class="choice">Paid</span>
</div>
<div class="form_item_row">
<label for="include_timelog'.$invoice_id.'">Include Time Log</label>
<input type="checkbox" value="true" '.$include_timelog.' name="include_timelog" id="include_timelog'.$invoice_id.'" />
</div>
<div class="clear"></div>
<div class="form_item_row_btns">
<input type="hidden" value="'.$invoice_id.'" name="hiddenInvoiceID"/>
<input type="submit" class="btn" value="Update Invoice" name="action"/>
</div>
</form>
$query = "UPDATE invoices SET status = ".$db->prep($_POST['status']).", include_timelog = ".$db->prep((isset($_POST['include_timelog'])?1:0))." WHERE invoice_id = ".$db->prep($invoice_id);
$(document).ready(function()
{
var action = '';
$(".due_date").datepicker();
$('input[name=action]').click(function(){
action = $(this).val();
});
$(".editInvoiceForm").submit(function() {
$('.editInvoiceForm .form_message').html('<img src="images/loadingAnimation.gif" alt="loadingAnimation" width="30" height="8"/>');
var dataToSend = {};
$(this).find(':input').each(function (i,el) {
dataToSend[el.name] = $(el).val();
});
dataToSend.action = action;
$.ajax({
type: "POST",
url: "process.php",
data: dataToSend,
dataType: "json",
cache: false,
success: function(data){
//console.log(data.status);
if(data.status == 'error'){
$('.editInvoiceForm .form_message').removeClass('status_green').addClass('status_red').html(data.message).append(data.script);
}else{
$('.editInvoiceForm .form_message').removeClass('status_red').addClass('status_green').html(data.message).append(data.script);
}
}
});
return false;
});
});
You need quotes around the value for status in your SQL since the value is a string. Your include_timelog and invoice_id values are integers and do not need quotes.
$query = "UPDATE invoices SET status = '".$db->prep($_POST['status'])."', include_timelog = ".$db->prep((isset($_POST['include_timelog'])?1:0))." WHERE invoice_id = ".$db->prep($invoice_id);