Save values in db with submit button only - php

I am trying to save some values into db, on first page i am running some matches and on the the 2nd page i need to save values. on first page only click button is shown to user, when he clicks , the values are stored.
code
<form action="ms_insert.php" method="post">
<input type="submit" value="Claim your daily bonus" name="test">
</form>
How can i submit all the values that are outside the form and send them to the ms_insert.php and process the values.
What i need to achieve it like this.
some values shall be matched , on successful match it will save the enteries into the db.
Here is the exact code that i am using now :
<?php
if ( $thismemberinfo[msurf] >= $msurfclicks ) {
$sql=$Db1->query("INSERT INTO d_bonus VALUES('$userid','0.02',NOW())");
echo "success";
}
else
{
echo "wrong";
}
?>
<form action="" method="post">
<input type="hidden" value="123" name="match">
<input type="submit" value="Claim your daily bonus $o.o2" name="claim">
</form>
I want this php code to excute when user click the submit button.

There are two ways that I can think of doing this:
Simply set the post variable to another value as described here:
$_POST['text'] = 'another value';
This will override the previous value corresponding to text key of the array. The $_POST is super global associative array and you can change the values like a normal php array.
Second would be to use _SESSION tags as described in this post:
In the first.php:
$_SESSION['favcolor'] = 'green';
In ms_insert.php:
echo $_SESSION['favcolor']; // green
P.S. You can also use cookies
Additional sources:
http://www.felixgers.de/teaching/php/hidden1.html

You can use Javascript with a XHR object for example or try to insert your values to store in hidden inputs.
Solution 1 : AJAX, for example :
Your JS (Here with Jquery):
function saveInDb(){
/* Retrieve your values in your page : */
var myValue1 = $('#value1Id').val();
var myValue2 = $('#value2Id').val();
/*Create a Jquery ajax object : */
$.ajax({
type: "POST",
url: "ms_insert.php",
data: { "value1": myValue1, "value2": myValue2 }
}).done(function( msg ) {
alert( "Data Saved");
});
}
Your HTML :
<span id="value1Id">My value 1</span>
<span id="value2Id">My value 2</span>
<button onclick=saveInDb()></button>
Solution 2 : the HTML with hidden inputs :
<form action="ms_insert.php" method="post">
<input type="hidden" value="My value 1" name="value1">
<input type="hidden" value="My value 2" name="value2">
<input type="submit" value="Claim your daily bonus" name="test">
</form>

Related

How to create a dynamic form in jquery+html+php?

For a project, I need to generate a dynamic html form that will send POST info to a PHP page via the ACTION field.
The form has not to be static, it has to be dynamic so the user has to be able to generate(ADD) a not fixed(dynamic) number of input tags, then when all the inputs are generated and filled the user may click on submit button and send all the info to a php document via post.
I'm completely lost
I've been playing with this piece of code that generates the inputs but I'm not able to send the data via post to the php file
<script>
var choices=[];
choices[0]="one";
choices[1]="two";
function addInput(divName){
var newDiv=document.createElement('div');
newDiv.innerHTML="<input type='text'>";
newDiv.innerHTML=newDiv.innerHTML+"</input>";
document.getElementById(divName).appendChild(newDiv);
}
</script>
<form class="new" method="post" action="action.php">
<div id="dynamicInput">
</div>
<input type="button" value="Add" onclick="addInput('dynamicInput');" />
<input type="button" value="Save" />
</form>
To submit the form, one possible scenario is to change the type of Save button to submit:
<input type="submit" value="Save" />
Also add name property to your auto generated inputs. Otherwise you won't access the submitted $_POST data:
<input name="enter_name[]" type='text'>
As you see, I'm concatenating [] to the input field. That will convert all submitted data into an array. Otherwise, that last one auto generated input will overwrite the previous entered data.
just change your input type to submit instead of save
<input type="submit" value="Save" />
and you don't need to create new div each time to create an input you can either append it to the div you already have
document.getElementById(divName).innerHTML+= "<input type='text' name= 'added_input[]'/>";
or
var newInput=document.createElement('input');
// if you want each input in separate lines
newInput.style.display = "block";
newInput.setAttribute("name","added_input[]");
document.getElementById(divName).appendChild(newInput);
and in your php file you can get post values like this :
for ($i = 0; $i < $_POST['added_input']; $i++){
echo $_POST['added_input'][$i];
}
for more option to get post values see This Question

jQuery making forms disappear and appear after each other

As the title says This is the code that I tried with. The forms must appear one by one because information from previous forms determine how the next ones will look.
$(document).ready(function(){
$('#first_form').submit(function(){
$('#first_form').fadeOut('fast');
$('#second_form').fadeIn('fast');
});
});
<form action="new_patch.php" method="POST" id="first_form">
Title: <input type="text" name="patch" placeholder="Patch 4.20">
<br/>
Number of Champions: <input type="number" name="champ_number" min="1" max="99">
<br/>
<input type="submit" value="submit">
</form>
<form action="new_patch.php" method="POST" id="second_form" style="display: none;" >
<input type="text" value="text">
<input type="submit" value="submit">
<?php
$champ_number = null;
if(isset($_POST['champ_number']))
{
$champ_number = $_POST['champ_number'];
for($champ_number;$champ_number>0;$champ_number--)
{
echo "<br/>Champion ".$champ_number."<input type=\"number\" name=".$champ_number." min=\"1\" max=\"99\">";
}
}
?>
</form>
You're mixing client-side and server-side form code. Submitting the form will reload the page entirely, so from the looks of your code it will fade in the new form when the old form is submitted, but then reload the page so the old form will show again anyway.
You could either:
Let the PHP determine how the next form appears based on the submission of the first form, e.g. if (isset($_POST["First_form_submit"]) { Show second form... }
Probably better and more user-friendly: make the second form appear below once the user has filled in the relevant inputs on the first form before they've submitted
you can use:
$('#first_form').submit(function(){
$('#first_form').fadeOut(function() {
$('#second_form').fadeIn('fast');
});
return false;
});
From the jQuery documentation the syntax is fadeIn( [duration ] [, complete ] ) it accepts a duration and a onComplete callback that you can use to execute the next action when the first is completed.
I did this once too, just add a submit class to the button and make it like this:
<input type="submit" value="submit" class="submit">
Change script to a click function.
$(document).ready(function(event){
event.preventDefault();
$('.submit').click(function(){
$('#first_form').fadeOut(400);
$('#second_form').fadeIn(400);
});
});
PS, also you need to prevent submit default...otherwise it will just submit the form, see this JSfiddle

can i submit a form without submit button or without hitting enter?

Can I submit value of <select> in php form without having submit button?
In my html/php code I am using two form or we can say two <select> as you can see in coding. In the second <select>(sub category) I want to use value of first <select>(main category) so that it can show me sub category of main <select>. but I am getting confused, how to do this on a single page without using js.
See my code:
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
main course name <select name ="bcate" ><option value=''>choose</option><?php $auth->cmaster_array(); ?></select>
</form>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
sub course name <select name ="sbcate" ><option value=''>select sub course</option><?php $auth->clmaster_array($frsbct); ?></select>
section name <input type="text" name="cname" required/>
remarks <input type="text" name="remarks" required/>
<input type="submit" name="section_master" value="Submit">
</form>
</body>
The thing you've asked that , You need value of first select input for second input values.
You can do this only by using jQuery without submitting form.
write change event of first select input box.
then send data to your server by jQuery ajax.
and then fill the second input box received from ajax
Code :
// To get value of select box
$('#select_box_id').change(function()
{
var select_box_one_value = $(this).val();
});
$.ajax
({
type: "POST",
url: HOST + "/path/to_your_controller_function",
data: 'data_of_select_box='+select_box_one_value,
cache: false,
success: function(msg)
{
}
});
using this , your controller will get the value of select box value and then return data from there , and you will receive that data in msg variable.
You can submit the form using javascript.
You can use this code to submit the form
document.getElementById("[ur form id should be here]").submit();
You can use following code
<form id="jsform" action="whatever you want">
// input fields
</form>
<script type="text/javascript">
document.getElementById('jsform').submit();
</script>

jQuery .on() with specific form, input, and data output

I would like to do this:
I have a bunch of buttons in a form. They all have unique names. Whenever someone CLICKS on any of these, in this specific form, I would like to submit an event (SUBMIT_IMAGES) and just get the $_POST variable or data of the clicked button and not of the entire form.
Something like this:
<form enctype="multipart/form-data" method="post" id="IMAGE_FORM">
<input value="" type="button" name="CLICKHERE1" class="IC10" onclick="SUBMIT_IMAGES();">
<input value="" type="button" name="CLICKHERE2" class="IC10" onclick="SUBMIT_IMAGES();">
</form>
$('#IMAGE_FORM:input').on('click', SUBMIT_IMAGES);
function SUBMIT_IMAGES(event)
{
var DATA = new FormData(event);
}
first, how can I specify .on event in a specific form, on an input in that form... is this correct?
$('#IMAGE_FORM:input').on('click', SUBMIT_IMAGES);
Secondly, how can I do something like:
function SUBMIT_IMAGES(event)
{
var DATA = new event.target.FormData();
}
but insead do a events.target.INPUTDATA(); kinda thing
Why don't you try AJAX ?
If your form looks like :
<form enctype="multipart/form-data" method="post" id="IMAGE_FORM">
[many inputs]
<input value="" type="button" name="CLICKHERE1" class="IC10" onclick="SUBMIT_IMAGES();">
[many inputs]
<input value="" type="button" name="CLICKHERE2" class="IC10" onclick="SUBMIT_IMAGES();">
</form>
Then you can try something like this :
$('input[name=CLICKHERE1]').click(function(){
var fields = '{ ';
$('#IMAGE_FORM').each(function(){
if($(this).name() == 'CLICKHERE1')
{
fields += ' }'// close json
}
else
{
fields += // feed fields var
}
});
$.ajax({
// Do some stuff
});
});
Then you can send datas using AJAX.
Of course, if you have to post images or other files it'll be quite difficult.

PHP AJAX within a table

I have a table that lists my students.. and the License Number Column will either shown the license number or if there is no number in the DB it will show a textbox..
Upon submit (note: no submit button, to keep it need i just press return)
The results from the PHP script will be shown via Ajax.
My complete code is here.
http://pastebin.com/9k0EKXA9
Here is the code within the license number cell on each row:
<td><?php // check if license number exists.
if($row['license_no'] == '')
{
//show form.
?>
<form method="POST" id="license_no_update"">
<input type="text" name="license_no" value="License Number" />
<input type="hidden" value="<?php echo $row['student_id']; ?>" name="student_id" />
</form>
<div id="output"></div>
<?php
}else{
//show license no.
echo $row['license_no'];
}
?></td>
Here Is the JQUERY
<script type="text/javascript">
$(document).ready(function() {
$("#license_no_update").submit(function() {
var license_no_update = $(this).serialize();
$.post('license_update.php', license_no_update, function(data) {
// We not pop the output inside the #output DIV.
$("#output").html(data);
});
return false;
});
});
</script>
The problem i am having, even after searching google many times..
I know i have to have a new form & element id for each row of the table.
but even when i do have those, i do not know how to get JQUERY to find that unique number..
currently with the code attached if i submit on the first row of the table, the correct results are displayed, if i submit on any other row nothing is displayed..
I hope that all makes sense.
Regards
Aaron
Use .find() to find the value of the hidden input within the clicked form. Notice the use of $(this) which is the form itself, and then you can narrow down the input by it's name, since you know the name.
However, it is unclear what you want to do with the id so I left that up to you.
$("#license_no_update").submit(function() {
var studentID = $(this).find("input[name='student_id']").val();
var license_no_update = $(this).serialize();
$.post('license_update.php', license_no_update, function(data) {
// We not pop the output inside the #output DIV.
$("#output-" + studentID).html(data);
});
return false;
});
Update:
Here is one way of creating unique ID's for each output:
if($row['license_no'] == '')
{
//show form.
?>
<form method="POST" id="license_no_update"">
<input type="text" name="license_no" value="License Number" />
<input type="hidden" value="<?php echo $row['student_id']; ?>" name="student_id" />
</form>
<div id="output-<?php echo $row['student_id']; ?>"></div>
<?php
}else{
//show license no.
echo $row['license_no'];
}

Categories