make sure at least one radio button is checked before submission - php

heres something that has bothered me for a while,i have several dynamically generated forms each with two radio buttons and a submit button,now the problem is that each time i submit a radio button selection,on click event is not fully consumed until another on change event happens,meaning that if the next form doesnt detect an onchange event on the radio buttons it uses the previous event
and produces a value(of the previous submitted radio button) even if no selection is made..
Check the code below,thanks
$(function(){
var pName;
var selectedValue;
var checked;
$(".sel1").on('change',function(){
$checked=$(this);
alert($checked.val());
});
$(".submit_btn").on('click',function(event){
event.preventDefault();
var $this=$(this);
var $cat_id=$this.data('uid');
alert($selectedValue);
$.ajax({
url:"vote.php",
method:"get",
data:"cat_id="+$cat_id+"&hisId="+$selectedValue,
success:function(result){
alert(result);
}
})
});
});
php code
<?php
}else{
?>
<div class="col-md-4 col-lg-4 col-sm-4 col-xsm-4 col-xxl-4 mt-4">
<div class="cat_holder">
<form action="" class="ml-4">
<div class="form-group">
<label for=".sel1" class="cat_name"><?php echo $catNames; ?></label><br>
<?php
foreach($player_id as $real_player_id){
$player_name=$caller->getPlayerNames($real_player_id);
?>
<input class="text_style sel1" type="radio" name="id" value="<?php echo $real_player_id;?>"> <?php echo $player_name;?><br>
<?php
}
?>
</select>
</div>
<div class="form-row">
<div class="col-md-6 col-lg-6 col-sm-6 col-xsm-6 col-xxl-6">
<input type="submit" value="Vote" class="btn btn-primary btn-block submit_btn" data-uid="<?php echo $realcatIds;?>">
</div>
</div>
</form>
</div>
</div>

Related

codeigniter get value after submit form

i have modal using form selection ajax with submit button
modal code
<div class="modal-body">
<div class="row" id="modal-body">
<div class="col-md-12">
<form class="form-horizontal">
<div class="box-body">
<div class="form-group">
<label for="project-loa" class="col-sm-4 control-label">Select Project</label>
<div class="col-sm-8">
<select id="project-load-modal" class="form-control" name="proj_name" style="width: 100%">
</select>
</div>
</div>
</div>
<!-- /.box-body -->
</form>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button class="btn btn-primary">Download</button>
</div>
my value selection option is fine
controller
public function generate(){
$proj_name = $this->input->get('proj_name');
echo $proj_name;
}
i want get value from my selection form using controller.
when the submit is click, i get nothing in my console. what i do wrong?
//give id to submit button button_id
$("#button_id").on('click', function () {
var proj_name = $("#project-load-modal").val();
$.post(base_url + 'generator_url'+proj_name, function (response) {
if (response.code == '1') {
//message if you want to show
} else {
//message if you want to show
}
});
}
//make route
$route['generator_url/(:any)']='ControllerName/generate/$1';
Add action and method attribute to form element
<form class="form-horizontal" action="<?php echo base_url().'C_MRCR_A/generate'; ?>" method="get">
Take submit button inside the form element.
<button type="submit" class="btn btn-primary">Download</button>
</form>

Radio Buttons return same value as the first dynamically generated radio button

i really need help with something...so im dynamically generating radio buttons from php,based on the number of items in the database,the radio buttons come with a value and a text,value is the id of the corresponding entry in the databse,the problem is when i try to get that value,it returns the value for the first radio button,for all the radio buttons,below is the jquery and php code i used,your help will be very much appreciated,Thanks
$(function(){
var pName;
var selectedValue;
$(".sel1").on('change',function(){
$selectedValue = $("input[name='id']:checked").val();
alert($selectedValue);
});
$(".submit_btn").on('click',function(event){
event.preventDefault();
var $this=$(this);
var $cat_id=$this.data('uid');
$.ajax({
url:"vote.php",
method:"get",
data:"cat_id="+$cat_id+"&hisId="+$selectedValue,
success:function(result){
alert(result);
}
})
});
});
php
<div class="col-md-4 col-lg-4 col-sm-4 col-xsm-4 col-xxl-4 mt-4">
<div class="cat_holder">
<form action="" class="ml-4">
<div class="form-group">
<label for="#select1" class="cat_name"><?php echo $catNames; ?></label><br>
<?php
foreach($player_id as $real_player_id){
$player_name=$caller->getPlayerNames($real_player_id);
?>
<input class="text_style sel1" id="select1" type="radio" name="id" value="<?php echo $real_player_id;?>"> <?php echo $player_name;?><br>
<?php
}
?>
</select>
</div>
<div class="form-row">
<div class="col-md-6 col-lg-6 col-sm-6 col-xsm-6 col-xxl-6">
<input type="submit" value="Vote" class="btn btn-primary btn-block submit_btn" data-uid="<?php echo $realcatIds;?>" readonly >
</div>
</div>
</form>
</div>
</div>
1.Remove id="select1" from radio buttons in your PHP code. (As id's need to be unique)
2.Convert
$selectedValue = $("input[name='id']:checked").val();
to
$selectedValue = $(this).val();
Note:-$("input[name='id']:checked") will give always the first checked checkbox value among all checked checkboxes.

Ajax form submission without refresh "not working"

I'm trying to submit the form in the same page without refresh the page.
my form:
<?php
if(isset($_POST['transfer'])){
$amount = $_POST['amount'] ? $_POST['amount'] : '';
$from_c = $_POST['from_c'] ? $_POST['from_c'] :'';
$to_c = $_POST['to_c'] ? $_POST['to_c'] : '';
}
?>
<form class="form-inline" method="post" action="convert.php" onsubmit = 'return false;' id = "frmData">
<div class="md-form form-group col-sm-3">
<div class="input-group col-sm-12">
<input type="number" value="1" min="0" step="0.01" data-number-to-fixed="2" data-number-stepfactor="100" class="form-control" name="amount">
<span class="input-group-addon" id="natCurrency" style="margin-top: 10%;margin-left: -5;margin-right: 15%;"></span>
</div>
</div>
<div class="">
<label for="from_c">from currency</label>
<select id="natSelect" onchange="let csymbol = $(this).find(':selected').data('symbol');$('#natCurrency').text(csymbol) " name="from_c" class="mdb-select colorful-select dropdown-primary" style="width:200px;" placeholder="select currency" >
<?php foreach ($black_a as $name => $black_p) {
?>
<option value="<?php echo $black_p['name'];?>" data-symbol="<?php echo $black_p['symbol_native']; ?>"><?php echo $black_p['name'];?></option>
<?php } ?>
</select>
</div>
<div class="">
<label for="to_c">to currency</label>
<select class="mdb-select colorful-select dropdown-primary" name="to_c" style="width:200px;" >
<?php foreach ($black_a as $name => $black_p) {
?>
<option value="<?php echo $black_p['name'];?>"><?php echo $black_p['name'];?></option>
<?php } ?>
</select>
</div>
<input name="transfer" value="convert" class="btn btn-elegant" style="margin-top:3%;" id = "ha" />
</form>
And I have this PHP code when the form is submitted:
<?php
if(isset($from_c) &&$from_c ==$to_c && isset($to_c) && $to_c == $from_c){
echo "
<div id='less' class='well'>
<div dir='RTL'>
<ul class='text-right'>
<li><strong>resultل</li>
</ul>
<div class='col-md-4'>
<div class='msg msg-success msg-success-text'> <span class='glyphicon glyphicon glyphicon-ok'></span> Error !</div>
</div>
";
}
?>
I tried using AJAX, the page is still refreshing after clicking submit:
$('#ha').on('click', function (e) {
e.preventDefault(); //prevent to reload the page
$.ajax({
type: 'POST', //hide url
url: 'convert.php', //your form validation url
data: $('#frmData').serialize(), //USE THE ID WE SET IN THE FORM
success: function (response) {
$(".mydiv").append(response);
}
});
});
.mydiv:
<div class='mydiv'>
<div dir='RTL'>
<ul class='text-right'>
<li><strong>result</li>
</ul>
<div class='col-md-4'>
<div class='msg msg-success msg-success-text'> <span class='glyphicon glyphicon glyphicon-ok'></span> $amount</div>
</div>
I also tried to using iframe but when clicking the submit button nothing happens.
How can I make this works and displaying the message I have in the if statement in my second PHP code?
This will work If this is your submit button
<input type="submit" name="transfer" value="convert" class="btn btn-elegant" style="margin-top:3%;" />
Change it to
<input name="transfer" value="convert" class="btn btn-elegant" style="margin-top:3%;" id = "ha" /> //ADD AN ID TO SPECIFY THE ON CLICK TRIGGER
Then your form
<form class="form-inline" method="post" action="convert.php" >
Change it to
<form class="form-inline" method="post" action="convert.php" onsubmit = 'return false;' id = "frmData"> // ADD AN ID TO YOUR FORM
Then minor adjustment to your script
$('#ha').on('click', function (e) {
e.preventDefault(); //prevent to reload the page
$.ajax({
type: 'POST', //hide url
url: 'convert.php', //your form validation url
data: $('#frmData').serialize(), //USE THE ID WE SET IN THE FORM
success: function (response) {
$(".yourDiv").append(response);
}
});
});
to achieve this you have to make changes in your code first change your form part like this
<form class="form-inline" method="post" action="convert.php" onsubmit = 'return false;' id = "frmData">
change your submit button like this
<input name="transfer" value="convert" class="btn btn-elegant" style="margin-top:3%;" id = "ha" />
then in your ajax function add e.preventDefault(); in first line .

dont refresh the page when you submit the form

hii guys im new to web programing and im creating a website
in this website i want to add a contact us form in the footer of the page like this
<form action="footer_contactus1.php" method="post">
<div class="row">
<div class="form-grope col-lg-4">
<label><span class="glyphicon glyphicon-user"></span>Name</label><br>
<input name="cuname" type="text" class="form-cotrol">
</div>
<div class="form-grope col-lg-4">
<label><samp class="glyphicon glyphicon-envelope"></samp>Email</label><br>
<input name="cuemail" type="email" class="form-cotrol">
</div>
<div class="form-grope col-lg-4">
<label><i class="glyphicon glyphicon-earphone"></i>Phone Number</label><br>
<input name="cutele" type="tel" class="form-cotrol">
</div>
<div class="form-grope col-lg-12">
<br>
<label><span class="glyphicon glyphicon-paste"></span>Message</label><br>
<textarea name="cumessage" class="form-cotrol" rows="6" style="width: 100%;" ></textarea>
</div>
<div class="form-grope col-lg-12">
<br>
<input type="hidden" name="save" value="contact">
<button type="submit" class="btn btn-info">Submit</button>
</div>
</div>
</form>
and when i click the submit button i want the php file to get executed so the inputs got inserted into the database
the php file
<?php
$pdo = new PDO('mysql:host=localhost;dbname=mywsite_db', 'root', '');
$req = $pdo->prepare('INSERT INTO message (id_message,nom,email,tele,message)
VALUES(null,?,?,?,?)');
$req->execute(array($_POST['cuname'],$_POST['cuemail'],$_POST['cutele'],$_POST['cumessage']));
header('Location: HomePage.php');
?>
i wanted this form to get submited without refreshing the page by ajax
i tried different tutorials and nothing worked can you plzz gives me the correct ajax script for my form so i can submit it to the php file and insert the data on the database then a message (alert) pop up telling me that the data has been inderted without refreshing the page plzz
<form action="footer_contactus1.php" method="post" id="myForm1">
...
</form>
<script type="text/javascript">
var frm = $('#myForm1');
frm.submit(function (ev) {
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
alert('The data has been inserted');
}
});
ev.preventDefault();
});
</script>

jquery append() function not working

I am creating my own custom chat bar all the related coding i have done, database and all the php functions working fine but when after submit the chat input message i am trying to append the input box data with jquery append function its not working at all.
html:
<div class="chat-bar-wrapper">
<div class="chat-bar-head">
<div class="chat-bar-icons-top">
<i class="fa fa-minus"></i>
</div>
<div class="chat-bar-title">
<p>Chat Online</p>
</div>
</div>
<div class="chat-bar-form">
<div class="chat-bar-text-display" id="messages_output"></div>
<form class="message-box" id="chat-form">
<div class="chat-bar-text-inputs">
<div class="chat-bar-text-input">
<input type="text" class="form-control" name="messages_input" placeholder="Enter your text here">
</div>
<div class="chat-bar-text-enter">
<input type="submit" class="btn btn-warning form-control" value="#">
</div>
</div>
</form>
</div>
</div>
js:
$(function(){
$("#messages_output").load('ChatMsgLoad.php');
$("#chat-form").submit(function(){
$.post('chatMsgPost.php', $('#chat-form').serialize(), function(data){
$('#messages_output').append(data);
});
return false;
});
});
Try this appendTo() instead of append().
$( data ).appendTo( "#messages_output");
And check in the console whether data variable contains something.
Read more here appendTo() in jQuery

Categories