So I am trying to make a select element update a MYSQL record every time the selection is changed. But the only way I was successfully able to trigger the PHP code is through an input button and not what is shown below. I need to be able to trigger the PHP code with the invisible input submit.
The whole point is to update PHP records through the select element and no button.
<form id="signin-form_id" class="panel" method="POST" style="margin-bottom: 0px;">
<div class="input-group">
<span class="input-group-addon">
Coin
</span>
<select class="form-control" name="cointype" onchange="this.form.submit()">
<option value="0" selected>Bitcoin</option>
<option value="1">Litecoin</option>
</select>
</div>
<noscript><input type="submit" name="ReferralCoin"></noscript>
</form>
And the submit button is suppose to trigger a POST event in PHP seen here:
Do not comment about MYSQL injection vulnerabilities.
<?php
if (isset($_POST['ReferralCoin']))
{
$cointype = $_POST['cointype'];
if($cointype == "0")
{
$odb->exec("UPDATE users SET `coin` = '$cointype' WHERE `ID` = '$userid'");
}
elseif($cointype == "1")
{
$odb->exec("UPDATE users SET `coin` = '$cointype' WHERE `ID` = '$userid'");
}
}
?>
The problem is if I was to use an input button it works fine and triggers the php POST code, but when using the invisible input submit it does not trigger the PHP POST code. If anyone can help me find my mistake, much is appreciated!
You're checking for the submit button here:
if (isset($_POST['ReferralCoin']))
But if the submit button isn't being clicked (since you're submitting the form via JavaScript code instead) then it isn't included in the POST values. So $_POST['ReferralCoin'] isn't set.
You'd need to remove the check entirely, or check some other condition. (Such as whether $_POST['cointype'] is set.)
Related
I want to show buttons depending on the values from a table. Afterwards I need to access anyone button and act according to the label of buttons. To be more specific, i have a table containing names of subjects, I want to show the buttons each having label of a subject. when user clicks a button (eg: Physics), the screen should display a test (MCQs) of Physics only, and so on:
Below is my code:
<button id="subj" onclick="showSubjects()">Start Test </button>
<div id="subjects" style="display: none;">
<form method='POST'>
<?php include_once('connect.php');
$query = "select distinct subject from subjects order by subject
desc ";
$result = mysqli_query($conn,$query);
while($row=mysqli_fetch_array($result)){
$subjectname = $row['subject'];?>
<input type="submit" id = "sub" name="sub"
value='<?php echo $subjectname; ?>'>
<?php } ?>
</form>
</div>
<?php
if(isset($_POST['sub'])){echo $subjectname; } else {echo
"Nothing";}
?>
</body>
<script type="text/javascript">
function showSubjects(){
document.getElementById("subjects").style.display = "block";}
function showTest(){
document.getElementById("tests").style.display = "block";
} </script>
This shows 3 buttons: PHISICS CHEMISTRY BIOLOGY
when user clicks any of these buttons it takes it as last button value i.e. BIOLOGY.
How can I amend it so that if user clicks CHEMISTRY it should show "Chemistry button clicked" and so on
The only thing that will change based on the submitted form is the $_POST array. Other variables, like $subject, will be the same whatever the user does; PHP isn't going to guess what you want them to be.
You've already got logic checking if the user pressed any button:
if(isset($_POST['sub'])){ ... }
Determining which button they pressed is just a matter of looking at that same variable:
echo $_POST['sub'];
The same goes for any other form controls you add - the user's selections will end up in $_POST, and it's up to you to read them from there.
It's also worth remembering that the logic for displaying the form doesn't need to be in the same place as the logic for precessing the submitted form, so the fact that these buttons are generated dynamically doesn't make a difference to how you read the submitted data back. You can't even guarantee that the user used your form at all, they could write their own and submit it to your server, or edit it using their browser's debugging tools.
I am looking for some help about how to make input form handling in PHP.
What I need is when a user writes data into a text form (table1), and moves to another text form (like pressing TAB, or selecting with mouse), then it should start and MySQL query to see if such data written at table1 already existing in the matching MySQL table.
My goal is like to do it without pressing submit button. Something like when google checks if an username you want to register already exists.
I am thinking about something like this:
$duplicate_data_test ="";
if (focus has moved to another form field - how to check ?) {
$query = "SELECT table1 FROM testdatabase WHERE table1 = "' . (table1 from the form below - how to get data from the this form field without POST?) .'";
$result = mysqli_query($con,$query);
if(mysqli_num_rows($result)>0) {
$duplicate_data_test = "This data is already found in the database. Choose something else";
}
}
echo '<form action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'" method="post">';
echo '<input type="text" maxlength="30" name="table1">';
echo '<span class="duplicaterror">'. $duplicate_data_test.' </span>';
echo '<input type="text" maxlength="30" name="table2">';
echo '<input type="submit" value="OK">';
echo '</form>';
Thank you very much for your help!
You cannot do your "interface" check with php.
Your "focus has moved to another form field" has to be done with javascript.
First, Build your form with html like this
<form name="testForm" action="postForm.php" method="POST" id="myForm">
<label>INPUT 1 : </label>
<input type="text" id="in1" value="" name="input1" />
<label>INPUT 2 : </label>
<input type="text" id="in2" value="" name="input2" />
<div style="color:red;font-weight:bold;" id="error"></div>
<button id="submitButton">SUBMIT</button>
</form>
Then make your checks when user clicks on submit button with javascript/jquery & ajax (prevent event form posting) like this :
$(document).on('click','#submitButton',function(event){
event.preventDefault();
if($.trim($('#in1').val()) == ''){
//input 1 is empty
$("#error").html('INPUT ONE IS EMPTY');
}//....continue checks
Finally, if your checks are good, then post your form
$("#myForm").submit();
and if your checks are not good then display user a message!
$("#error").html("MESSAGE!");
I made you a little example on how to do it (it's not the best way to do it but it's just an example) on jsfiddle, check this link : http://jsfiddle.net/9ayo89jt/2/
hope it helps!
checking if something exists will need an AJAX call
put the query that checks the database in a separate php file and call it with AJAX
to submit once all input fields are filled, you will need to use javascript .. check if field 1,2,3,..etc. are not empty .. formName.submit()
this is a bad approach in my opinion
i got a bit of a problem, as you guys can see in my code, i got a foreach looping through my img folder, showing all the images, well i left out the proper styling of the images, since the problem is the form inside every image.
i need to get the value from the radio input, so i can tell my other script to select all the info from the DB where the ID has the same value as the input.
and i needed a if statement so that if nothing is set i can show something saying you need to select a radio before you can listen to any stations.
but my if statement keeps showing 'error', even tho i click on the buttons
i hope anyone could help me out :)
if(isset($_POST['submitRadio')):
$test = $_POST['radio'];
echo $test;
else:
echo 'error';
endif;
foreach(glob('img/radios/big/*.png') as path):
printf('
<form method="post">
<input class="hidden" type="text" name="radio" value="1">
<input type="submit" name="submitRadio" value="Go!">
</form>
');
endforeach;
after doing what GolezTrol told me to do:
<div class="row text-center">
<?php
$sql = "SELECT * FROM radio";
$stmt = $Db->dbh->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
if(isset($_POST['submitRadio'])):
$test = $_POST['submitRadio'];
echo $test;
else:
echo 'error';
endif;
?>
<form method="post">
<?
foreach($result as $radio):
printf('
<button type="submit"
name="submitRadio"
value="'.$radio['id'].'">
Go!
</button>
');
?>
</form>
I've removed my previous solution. I only just saw that you have no actual radio button but hidden elements, so basically the buttons are the radios.
That changes it a little bit because you actually have the alternative suggestion I proposed earlier: The buttons themselves work as radiobuttons that immediately post the chosen option.
To solve your issue, you can use a button instead of an input for submitting the form. In a button, you can make the caption and the value differ from each other, so you can just put the id (or filename, or whatever you need) in the value of the button and don't need the hidden input at all. The content of the button tag is the caption it gets, so the code can look like this:
if(isset($_POST['selectedFile')):
$test = $_POST['selectedFile'];
echo $test;
else:
echo 'error';
endif;
?><form method="post"><? // Open the form outside of the loop
foreach(glob('img/radios/big/*.png') as path):
// Not sure what you want to do here. Put the filename instead of
// "1" in the value?
$filename = basename($path, '.png');
?>
<button type="submit"
name="selectedFile"
value="<?=$filename?>">
Go!
</button>
<?
endforeach;
?></form><? // Close the form
In this example I've pulled the form tags out of the loop, because strictly you only need one form. But since there are now no radiobuttons at all, you could also have one form per button. It would still work.
By the way, I saw you mentioned a database in your comment. Of course I didn't take that into account, since you didn't mention that in the question. Anyway, it shouldn't change the basic concept, only the way the 'value' is determined.
First, the value code in radio tag is always 1. So if any radio button is selected, you will always gets 1. Instead the value clause should have the filename. So you get to know which image (radio button) is selected.
For none of the image (radio button) selected, you may use
if (!isset($_POST['radio']))<br>{<br>//php code }
I have a form called choose_dates.php that submits to a file called process.php. The form consists of a textbox, a dropdown list and a submit button. I have set it up so that you can submit either one value, or the other, or both at the same time. I would like to have it such that if the user has put a value in the textbox AND the dropdown list, then a prompt will ask if that is what he/she really wants to do. The code below doesn't seem to do that when the submit button is pressed. The rest of my code (that I have not placed on here) works fine, this is more of a user interface issue.
<form name="dates" action="process.php" method="POST">
<input type="text" name="submitDate">
<select name="removeException">
<option value="some-value">display dropdown stuff</option>
.
.
</select>
<input type="submit" value="submit"
<?php
if($_POST['submitDate'] != "" and $_POST['removeException'] != "")
{
echo " onclick=\"return confirm('Are you sure you want to submit both values at the same time?')\" ";
}
?>
tabindex="2">
</form>
And of course, please ask any questions if what I said isn't clear enough. Regards.
Add onsumbit="return checks();" in form tag.
checks is a Javascript function that verify everything is good, if not, return false and the form will not be submited. If true, the form will be submited normally. just move your onclick to onsumbit in form.
You need to do that on the client side using javascript ( preferably ). The post data will be submitted when the form is submitted. Try adding this function as your form's onsubmit event
function func(){
var a = document.getElementsByName('removeException'),
b = document.getElementsByName('submitDate');
if(a[0].value!=null && b[0].value!=null){
var c = confirm('Are you sure you want to submit both values at the same time?')
if(c){
return true;
}else{
return false;
}
}
}
Then
<form name="dates" action="process.php" method="POST" onSubmit='return func()'>
I' new to HTML and PHP and was wondering if anyone out there could help me with a question... I'm trying to code a form that submits a selection from one menu OR another and, depending on which is chosen sends the data to one of two different PHP pages.
<form id="form1" method="post" action="">
<label for="SubjectID">Text Books by Subject</label>
</p>
<p>
<select "name="SubjectID" id="SubjectID">
<?php do { ?>
<option value="<?php echo $row_rsSubject['SubjectID']?>"><?php echo $row_rsSubject['SubjectName']?></option>
<?php } while ($row_rsSubject = mysql_fetch_assoc($rsSubject));
$rows = mysql_num_rows($rsSubject);
if($rows > 0) {
mysql_data_seek($rsSubject, 0);
$row_rsSubject = mysql_fetch_assoc($rsSubject);
} ?>
</select>
</p>
<p>
——— OR ———
</p>
<p>
<select name="CourseID" id="CourseID">
<?php do { ?>
<option value="<?php echo $row_rsCourse['CourseID']?>"><?php echo $row_rsCourse['CourseID']?></option>
<?php } while ($row_rsCourse = mysql_fetch_assoc($rsCourse));
$rows = mysql_num_rows($rsCourse);
if($rows > 0) {
mysql_data_seek($rsCourse, 0);
$row_rsCourse = mysql_fetch_assoc($rsCourse);
} ?>
</select>
</p>
<p>
<label for="CourseID">Text Books by Course</label>
</form>
So, the action should be (depending on which menu the user selects from) that the form submits to either subject.php or course.php, and I can't figure out how to do that with a single submit button. Can anyone help me?
first set onchange event to select:
<select name="CourseID" id="CourseID" onchange='myFunc(this)'>
Then:
<script>
function myFunc(element){
// set the form action depends on option chosen
// element.setAttribute('action', 'yourPageLink' );
}
</script>
You could update the action attribute with javascript when the user changes his selection, directing him to the relevant page.
Otherwise, why not submit to a single function and depending on the users selection call the relevant function?
Honestly I would use jquery's $.post to do this. To the best of my knowledge you can't really do that with straight html, and php is executed before the page loads so that's no help. With JQuery you could set an action to the button to call a function. Then your function could check the value of the select and pass the proper url to the $.post function.
You can read about that particular jquery function here: http://api.jquery.com/jQuery.post/
If you're not familiar with JQuery it's really easy to use with just a bit of reading if you're good with JavaScript.
Why not have a radio button in the form whose posted form value is processed by a central PHP form handler? That way, depending on what is entered on the radio button, you can process the request as necessary.
This might not be the answer you are looking for, but I am not clear if you want the user to be able to alter the destination or what.