i have been trying to find my way around this issue, I have used
$(function(){
$('form').submit(function(event){
event.preventDefault();
window.location = $('input[type=radio]:checked').val();
});
});
but all that does is redirect me to the page, I want to have a radio button that is selected redirect me to a page and then submit the form data to the div on that page and that page only, heres the form
<form name = "quoted" method="get">
<input id = "poster" type="text" name="poster" required="required" placeholder = "Credited Individual."> <br>
<textarea class = "actual_quote" name = "actual_quote" required="required" placeholder = "Write the question here!"></textarea><br><br><br>
<div class = "checkboxes" required="required">
<h3 style = "margin-top:-20px;">Please select one catagory that the quote falls into.</h3>
<label for="x"><input type="radio" name="x" value="stupid.php" id = "x" checked="checked" /> <span>stupid</span></label><br>
<label for="x"><input type="radio" name="x" value="stupider.php" id = "x" /> <span>stupider</span> </label><br>
<label for="x"><input type="radio" name="x" value="stupidest.php" id = "x"/> <span>stupidest</span></label>
</div>
<input id = "submit1" type="submit"><br>
</form>
and heres the div where we $_GET the data from the form and post it in the div
<div class="top-submit"><?php echo '“' . (!empty($_GET['actual_quote']) ? $_GET['actual_quote'] : '') . '”'; $actual_quote = $_GET['actual_quote'];?>
</div>
<div class="poster"><?php echo "-" . (!empty($_GET['poster']) ? $_GET['poster'] :''); $poster = $_GET['poster'];?>
<div class = "like">
Like
<p id = "like" style = "color:green;">0</p>
</div>
<div class = "dislike">
Dislike
<p id = "dis" style = "color:red;">0</p>
</div>
</div>
<?php
"INSERT INTO submissions(top-submit, poster)
VALUES ($actual_quote, $poster)";
?>
</div>
</div>
I have been stuck on this issue for a day, and I can't get out, please help!
if it's TL; DR
I want to be able to have one radio option selected, and when the user presses submit I want them to be redirected to the page of the radio option and then i want the data to be posted there and no where else. Please help! Thanks in advance -Connor
You are redirecting but not including the posted data.
It sounds like you should create a hidden form that the users don't see, and when they submit the form you have now, dynamically fill in the needed inputs and then trigger that form, posting the data to the correct page.
See the second answer here: pass post data with window.location.href
Related
As you can see in the picture I have multiple button inside one FORM so when I click on button It insert always the last value :: S (126).
This is my code:
<form method="post" action="" enctype="multipart/form-data">
<input type="text" class="form-control" placeholder="Numéro de chassit" name="chassit" id="chassit" >
<?php
if (mysqli_num_rows($result) > 0)
{ ?>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<?php echo "<img alt='' src='../admin/image/mercedes/".$row['image']."' >";?>
<div class="card-body">
<input id="model" name="model" type="hidden" value="<?=$row['model'] ?>">
<input type="submit" name="mo" class="btn btn-secondary" value="<?=$row['model'] ?>">
</div>
</form>
And this is the process
<?php
if(ISSET($_POST['mo'])){
$demandeur = $_SESSION['username'];
$model = $_POST['model'];
$chassit = $_POST['chassit'];
$sql = "INSERT INTO tbl_xp_support (demandeur,model,chassit)
VALUES ('$demandeur','$model','$chassit')";
if (mysqli_query($con, $sql)) {
header("Location: ../xp_group.php");
} else {
echo "Error: " . $sql . " " . mysqli_error($con);
}
mysqli_close($con);
}
?>
The input name="model" it insert always the last value !!!
Because your all input fields has same name, so last one will overwrite any previous ones. If you want to select only one that was selected, you should make it as radio button:
<label class="card-body">
<input id="model" name="model" type="radio" style="display: none;" value="<?=$row['model'] ?>"/>
<input type="submit" name="mo" class="btn btn-secondary" value="<?=$row['model'] ?>">
</label>
Please note:
you never use $_POST['mo'] value that should contain selected model
You have same ID in every iteration, and that is invalid HTML
Your browser has no way to know that each submit button is supposed to be associated with a single hidden input, since they're all part of the same form. What will actually happen is that regardless of which button is pressed, all of the hidden fields will be submitted.
Since they all have the same name, but different values, PHP has to decide which value to actually put into the $_POST array. Its policy is to take the last value, which is what you see.
There are a few ways I can think of to make this work:
Put each hidden field and submit button into its own HTML <form>, rather than having one for the whole page.
Have a single hidden field at the bottom of the page, and write some JavaScript that updates that hidden field when you click a button, before submitting the form.
Look at the value of the submit button by examining the $_POST['mo'] variable, and get rid of the hidden fields completely. But beware that there are some extra cases you need to consider when doing this.
I have an autocalc running on a form and the total is shown through a div ID. I need to be able to somehow render the div ID answer into a php form mailer.
I’ve tried adding a hidden input field with the div ID.
<div id="totalPriceTM">
<input name="Score" id="totalPriceTM" value="" type="hidden" />
</div>
No error messages but result not showing in email.
You should create different div name on your form.
for example :
<script>
function autocalc(x,y)
{
a = x*y;
document.getElementById(totalPriceTM).innerHTML = a;
document.getElementById(totalPriceTM2).value = a;
}
</script>
<div id="totalPriceTM">
<input name="Score" id="totalPriceTM2" value="" type="hidden" />
</div>
result will be show on div totalPriceTM and will be set the value of div totalPriceTM2
How to I get the input id of selected button, and post it from next php page? I have this code inside my form it's dynamically populate from my database.
<?php
include('connection.php');
$query = "Select * from tblproduct where categoryID = 1 and statusProd = 1";
$result = $conn->query($query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id=$row["ID"];
$product=$row["product"];
$image=$row["images"];
echo '
<div class="col-sm-4 divProduct" style="outline: none;background-color: transparent;border:none;height:320px;width:300px">
<div class="content">
<input type="submit" name="btnGame" id="btnGame" class="btnSubmit" value="">
<input type="hidden" name="prodID" value="'.$id.'">
<div class="btnBuyBuy text-center">
<img src="'.$image.'" class="imgProd" style="width:200px;height:170px;">
<br><br>
<label class="nameProd">'.$product.'</label>
<br><br>
<div class="divBuy2">
<label class="lblBuy">BUY NOW</label>
</div>
</div>
</div>
</div>';
}
}
And here's my code to the next page where the id displaying.
<?php
if (isset($_POST['btnGame'])) {
$id = $_POST['prodID'];
echo "$id";
}
else{
echo "failed";
}
And the output is the last id from my tblproduct which is 13. How can I get the id of selected button?
<input type="submit" name="btnGame" id="btnGame" class="btnSubmit" value="">
<input type="hidden" name="prodID" value="'.$id.'">
You aren't trying to get the "id" (you mean value) of the button, you are trying to get the value of the hidden input next to it.
The problem with this is that proximity to the clicked submit button means nothing. All hidden inputs will be successful controls. All will be submitted to the server. Since they all share the same name, only one of them will show up in $_POST. (If you renamed them so the name ended in [] then they would all should up as an array and you still couldn't tell which was selected).
Don't use a hidden input for this.
If you care about the button that is used, then make use of the submit button.
Only the submit button used to submit the form will be successful, so its name and value can be used to tell which one was clicked.
<input type="submit" name="btnGame" id="btnGame" class="btnSubmit" value="$id">
… and then look at $_POST['btnGame'] instead of $_POST['prodID'].
Given that you have value="" I'm guessing that you don't want any text displayed on the image and that you are using CSS background image to display something in it.
Obviously, the above won't be compatible with this, so use a <button> element instead.
That allows you to have a different label and value.
It also allows you to put elements inside the label, so you can use a content image with an alt attribute instead of a background image and score a big accessibility win.
<button name="btnGame" id="btnGame" class="btnSubmit" value="$id">
<img src="/path/to/icon.png" alt="Select product number $id">
</button>
I'm really struggling with an RSVP form I'm trying to set-up and any help would be great!
I have this form so far http://adrianandemma.com/ which I am trying to get to send me a simple email once submitted.
The form has a 'Name' field and a radio button for 'Attending - Yes/No'.
I then have some JS whereby you can clone these fields to RSVP for more than one guest at a time.
The 'Name' fields are passing through fine as an array and coming through by email, as I can just set the name attribute of the input to name="name[]", but I'm having trouble with the radio buttons.
I can't leave the 'name' attribute the same for the cloned radio buttons, because if I do I can only select yes/no for one cloned row, as all the cloned radios have the same name, so I have added a bit of JS to try to amend the name of any cloned radios to 'coming[1], coming[2], etc'.
I can't quite get this to work though, as every time I submit the form the radio button values appear to come through blank.
Can anybody advise the best approach to setting up radio buttons as an array and to carry them through via $_POST and ultimately an email script?
Here's my HTML form:
<?php
if(#$_REQUEST['submit'] == '1') {
include('assets/forms/rsvp.php');
}
?>
<form action="?" method="post">
<?php if(#$errors) :?>
<p class="errors"><?php echo $errors; ?></p>
<?php endif; ?>
<input type="hidden" name="submit" value="1" />
<div class="form-row">
<div class="field-l">
<p>Name</p>
</div>
<div class="field-r">
<p>Attending?</p>
</div>
</div>
<div class="form-row guest">
<div class="field-l">
<input type="text" name="name[]" id="name" value="<?php echo htmlspecialchars(#$_REQUEST['name']); ?>" tabindex="1" />
</div>
<div class="field-r">
<input type="radio" name="coming" id="coming-yes" class="coming-yes" value="Yes"><label for="coming-yes">Yes</label><input type="radio" name="coming" id="coming-no" class="coming-no" value="No"><label for="coming-no">No</label>
</div>
</div>
<a class="addguest" href="#">Add further guest</a>
<div class="form-row">
<button type="submit" id="rsvp-submit" tabindex="2">Submit RSVP</button>
</div>
</form>
Hers My Form Process code:
<?php
$name = $_POST['name'];
$coming = $_POST['coming'];
$errors = "";
if(!#$_POST['name']) { $errors .= "Please enter your name.<br/>\n"; }
if(!#$_POST['coming']) { $errors .= "Please enter yes or no for attending.<br/>\n"; }
if(#$_POST['emailaddress'] != '') { $spam = '1'; }
if (!$errors && #$spam != '1')
{
$to = "xxx#example.com";
$subject = "Wedding RSVP";
$headers = "From: noreply#adrianandemma.com";
$body = "The following RSVP has been sent via the website.\n\n";
for($i=0; $i < count($_POST['name']); $i++) {
$body .= "
Name ".($i+1)." : " . $_POST['name'][$i] . "\n
Coming ".($i+1)." : " . $_POST['coming'][$i] ."\n\n";
}
$body .= "\n\nDate Received: " . date("j F Y, g:i a") . "\n";
mail($to,$subject,$body,$headers);
}
?>
Here's my JS:
$(document).ready(function() {
$('.addguest').on('click', function(e) {
e.preventDefault();
//
// get the current number of ele and increment it
//
var i = $('.guest').length + 1;
$('.guest').first().clone().find("input").attr('id', function(idx, attrVal) {
return attrVal + i; // change the id
});
$('.guest').first().clone().find("input[type=radio]").attr('id', function(idx, attrVal) {
return attrVal + i; // change the id
}).attr('name', function(idx, attrVal) {
return attrVal+'['+i+']'; // change the name
}).val('').end().find('label').attr('for', function(idx, attrVal) {
return attrVal + i; // change the for
}).end().insertBefore(this);
});
});
Here's an example of what I'm receiving by email currently, names come through fine, but radio values for 'Coming Yes/No" are all blank:
The following RSVP has been sent via the website.
Name 1 : John Doe
Coming 1 :
Name 2 : Ann Doe
Coming 2 :
Name 3 : Fred Doe
Coming 3 :
Date Received: 19 April 2017, 1:04 am
Honestly, my best guess is that in the original row, the names of your radio inputs are simply "coming", without the brackets. I think that because there are no brackets on that name, it is clobbering the other ones of the same name that should behave as an array. In other words, PHP is getting two conflicting types for an input of the same name, and taking the string over the array.
Hard to say without testing it directly, and the fact that the input is referenced as an array in your PHP form handler and doesn't throw an error would tend to suggest to me I'm not quite right, but it may be worth a try.
Here's the change to the HTML I would try:
<div class="field-l">
<input type="text" name="name[0]" id="name" value="<?php echo htmlspecialchars(#$_REQUEST['name']); ?>" tabindex="1" />
</div>
<div class="field-r">
<input type="radio" name="coming[0]" id="coming-yes" class="coming-yes" value="Yes">
<label for="coming-yes">Yes</label>
<input type="radio" name="coming[0]" id="coming-no" class="coming-no" value="No">
<label for="coming-no">No</label>
</div>
Notice I specifically marked the first row as row zero, as PHP uses zero-indexed arrays.
This would then require some changes to your javascript. I've found it was easier to create an actual template for your row HTML and use that than to try and clone the first row each time and reset all the inputs and adjust the names. The way this works is you define your template HTML inside a script tag with an ID and a type that is non-standard. The browser ignores it, but JavaScript can access it just like any other element and we can pull the content out with jQuery's html() method.
Here's what I've come up with (including a fix of your indexing):
<!-- A script with a non-standard type is ignored by the browser -->
<!-- We can reference it by ID in our JS though, and pull out the HTML -->
<script id="guest-row-template" type="text/template">
<div class="form-row guest">
<div class="field-l">
<input type="text" name="" id="name" class="name-ipt" />
</div>
<div class="field-r">
<input type="radio" name="" id="" class="coming-yes coming-yes-ipt" value="Yes" />
<label for="" class="coming-yes coming-yes-label">Yes</label>
<input type="radio" name="" id="" class="coming-no coming-no-ipt" value="No" />
<label for="" class="coming-no coming-no-label">No</label>
</div>
</div>
</script>
<script type="text/javascript">
$(document).ready(function() {
$('.addguest').on('click', function(e) {
e.preventDefault();
//Get the number of rows we have already - this is the index of the *next* row
//If we have 1 row, the first row's index is 0 and so our next row's index should be
//1, which is also our length, no need to increment
var i = $('.guest').length;
//Get HTML template content for a single row
var row = $('#guest-row-template').html();
//Update the name attribute of the name input
row.find('.name-ipt').attr('name', 'name[' + i + ']');
//Update the name and id attributes of the yes radio button
row.find('.coming-yes-ipt').attr('name', 'coming[' + i + ']');
row.find('.coming-yes-ipt').attr('id', 'coming-yes-' + i);
//Update the name and id attributes of the no radio button
row.find('.coming-no-ipt').attr('name', 'coming[' + i + ']');
row.find('.coming-no-ipt').attr('id', 'coming-no-' + i);
//Update the for attribute of the yes label
row.find('.coming-yes-label').attr('for', 'coming-yes-' + i);
//Update the for attribute of the no label
row.find('.coming-no-label').attr('for', 'coming-no-' + i);
row.insertBefore(this);
});
});
</script>
Please note this is untested code. Of course I've gone through a few times to make sure I caught all my obvious bugs, but others may persist. Since I can't actively test it, I can't say it's entirely bug free. But, hopefully as pseudo-code it helps you resolve the issue.
EDIT 1
Just to clarify, you don't ordinarily have to manually provide the index values within the brackets of input names for PHP to interpret the input as an array and to automatically index the input in the appropriate order. I specifically set the first inputs to have use [0] because all the inputs after them will also need to specify index values in order for your radio buttons to work (I personally appreciate consistency), and because we need to be absolutely sure that the correct names are matched to the correct RSVP value (just trying to be thorough).
Hey Fellow Programmers,
I have a slight problem and I cant find the right answer online.
Basically what I need to do is, a user inserts content into a text box and then selects a check box. Whichever check box is selected is what table the text box content is supposed to insert into. **Both check boxes can be selected so the user can upload to two diff tables, before you ask no I cannot just upload to a diff row it has to be a completely diff table.
Let me know if I am not clear, and thanks in advance
HTML CODE:
<body class="login">
<div class="wrapper">
<h1><img src="img/logo-big.png" alt="" class='retina-ready' width="59" height="49">FLAT</h1>
<div class="login-body">
<form action="db_pre_panel.php" name="login" class='form-validate' id="test" method="post">
<div class="control-group">
<div class="email controls">
<h3>TEST</h3>
<input type="text" name="content" maxlength="500" placeholder="Content" />
</div>
</div>
<div class="control-group">
<input type="checkbox" name="Ck_1" /> <label>Ck_1</label>//If selected then INSERT content into tbl_connect
<input type="checkbox" name="Ck_2" /> <label>Ck_2</label>//If selected then INSERT content into tbl_share
</div>
<div class="submit">
<input type="submit" value="Simplicity" />
</div>
PHP CODE:
<?php
//Define Content, Post & Share
$content=$_POST['content'];
$post=$_POST['ck_1'];
$share=$_POST['ck_2'];
//Insert into the db
$sql_post="INSERT INTO $tbl_connect (wall) VALUES ('$connect', '$post')";
$sql_share="INSERT INTO $tbl_share (wall) VALUES ('$connect', '$share')";
//Make sure it insert into db
$result_post = mysql_query($sql_post);
$result_share = mysql_query($sql_share);
if($result_post){
header("location:alert.php");
}else{
header("location:error.html");
}
if($result_share){
header("location:http://www.google.com");
}else{
header("location:error.html");
}
?>
Just keep it simple:
//Define Content, Post & Share
$content = $_POST['content']; // you should sanitize this to prevent SQL injection
if ( !empty($_POST['ck_1']) ) {
$sql_post = "INSERT INTO `tbl_connect` (wall) VALUES ('$connect')"; // if you have more than one value, then you need to specify more than one column...
}
if ( !empty($_POST['ck_2']) ) {
$sql_share = "INSERT INTO `tbl_share` (wall) VALUES ('$connect')";
}