How to update database with ajax textarea - php

I can't figure it out how I can update the database with this textarea. Can somebody help?
The ajaxcall
$$('.btn').addEvent('click', function() {
var request = new Request( {
url: '<?php echo $baselink;?>',
method: 'post',
onSuccess:function(responseText) { alert(responseText);},
data: {
'name' : this.id,
'value' : this.value,
'tmpl':'component',
'format':'raw',
'ajax' : 1
}
}).send();});
**//Form//**
$s6=$item['Select6'];
$id=$item['items_id'];
print '<form method="post" class="formulier">
<input maxlength="250" NAME="name" class="name" id="'.$id.'" value="'.$s6.'" SIZE="50">
<input type="submit" value="Click me" class="btn"/></form>';
Query
if(JRequest::getVar('ajax') ) {
$state=JRequest::getInt('value','oeps');
$id=JRequest::getVar('name','');
if ( $id ) {
$state=(int)$state;
$query="UPDATE #__dataitems set `Select6`='".$state."' where `items_id`=".$id;
$db->query();
echo ' Bijgwerkt naar '.$state.' '.$id;
exit;}

You are currently listening on the radio button click and send the event when it does.
You need to add a button/href to your form and bind an click event to it, and then once it is clicked, just collect the data from the radio button and the textarea and send it:
HTML:
<input type="button" value="Click me" class="btn"/>
JS:
$$('.btn').addEvent('click', function(){
var radioId = ...//get radio id
var radioVal = ...//get val
var textarea = ... //get textarea val
var request = new Request( {
url: '<?php echo $baselink;?>',
method: 'post',
onSuccess:function(responseText) { alert(responseText);},
data: {
'volgorde' : radioId,
'check' : radioVal,
'textarea' : textarea ,
'tmpl':'component',
'format':'raw',
'ajax' : 1
}
}).send();});
I'm guessing you are using prototypejs (because of the $$), i don't know it very well so i can't help you with how to get the elements id and values, but this is the direction.

Related

how to multickeck in ajax call

multi checkbox not working in ajax calling it will reset,my checkbox like
<form action="#">
<?php
foreach($rimdiameter as $rows) {
if($rows->rimdiameter!='') {
?>
<p>
<label>
<input type="checkbox" name="diametersearchs[]" value="<?php echo $rows->rimdiameter; ?>" class="ads_Checkbox_diameter" id="diametersearch" multiple>
<span><?php echo $rows->rimdiameter; ?></span>
</label>
</p>
<?php } }?>
</form>
click a check box it will run a query and view the result and click next checkbox first will be unchecked ,my ajax code
$('.ads_Checkbox_diameter').change(function(){
$('input[type="checkbox"]').not(this).prop("checked", false);
var final = '';
$('.ads_Checkbox_diameter:checked').each(function(){
var final = $(this).val();
var type = 'diameter';
$("#loadMore").hide();
var siteurl = '<?php echo $root = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/'; ?>';
$(".loadingimageproduct").show();
var serachfilterproductbyprice = 'serachfilterproductbyprice';
var centre_bore='<?php echo $centre_bore;?>';
var bolt_pattern='<?php echo $bolt_pattern;?>';
var widtz=sessionStorage.getItem('widthz');
sessionStorage.setItem('diameter',final);
$.ajax({
url:"{{url('/')}}/serachfilterproductbyprice",
method: 'POST',
data: {serachfilterproductbyprice:serachfilterproductbyprice ,width:widtz, siteurl:siteurl, type:type, final:final ,bore:centre_bore ,pattern:bolt_pattern , "_token": "{{ csrf_token() }}"},
success: function(data) {
$('#msgproductfilter').html(data);
$(".loadingimageproduct").hide();
$(".front_product_wheel_show").hide();
$.ajax({
// save another data
});
}
});
});
});
why the checkbox unchecked if another one is clicking time ,how to solve?i wand multi checking property . and it will sore in array
The very first thing you do in your change handler is explicitly un-check all other checkboxes:
$('input[type="checkbox"]').not(this).prop("checked", false);
If you don't want to do that, simply remove that line of code.

onclick function using ajax

In my code having two radio buttons in form like
<input type="radio" required="required" id="bt" name="bt" value="1" checked="checked" onclick="window.location='<?php echo $_SERVER['REQUEST_URI']; ?>';" /> <span>Test1</span>
<input type="radio" required="required" id="bt" name="bt" onclick="Clear()" value="2" /> <span>Test2</span>
when i click on Test2 radio button the form having value will clear, for that i wrote code its working fine
<script>
function Clear() {
document.getElementsByName("one")[0].value = "";
document.getElementsByName("two")[0].value = "";
document.getElementsByName("three")[0].value = "";
}
</script>
with this form with values cleared.But when i click on "Test1" radio button the form values will come automatically.right now iam sessions using
onclick="window.location='<?php echo $_SERVER['REQUEST_URI']; ?>';"
for this it is coming but page is refreshing how to take it this code to ajax, i mean need to get values when i click on radio button without refreshing the page
Yes you can use window.location.href in ajax as:
$.ajax({
type: 'POST',
async: false,
url: '/users',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(payload),
success: function(data, textStatus, jqXHR){
console.log(jqXHR.status);
window.location.href= "/someUrl.html";
}
});
Note that: window.location.href will not reload the page if there's an anchor (#) in the URL
Here i wrote code for getting values of radio button without page refresh:
html code:
<input type="radio" name="bt" id="bt" onclick = "MyAlert()" value="blue"/>Blue <br/>
<input type="radio" name="bt" id="bt" onclick = "MyAlert()" value="red"/>Red
ajax script:
function MyAlert() {
var radio =$('input[type="radio"]:checked').val();
var pass_data = {
'radio' : radio,
};
$.ajax({
url : "radio-value.php",
type : "POST",
data: pass_data,
success : function(data) {
// alert radio value here
alert(data);
}
});
return false;
}
radio-value.php file:
<?php echo $bt =$_POST['radio']; ?>
Hope this helps.

Get the value of button and save in db on click

I want to update a field in db as 1 or 0 when i click on yes or no button..
<td> <input type="button" onClick="save();" value="Yes">
<input type="button" onClick="save();" value="No">
</td>
This code displays the button in view page as yes and no. Now what should i do to save in db while clicking on yes button?
<div class="bit-4 pad-small" style="float:none;">
<br>
<br>
<?php echo $this->Form->input('approve', array(
'type'=>'checkbox',
'style' => 'float:left;',)
) ); ?>
<span>required</span>
</div>
in your cakephp file you can get the value and update it to database. Hope you can get any way to update it now. Even you can do that in save() method.
/please change input to any unique selecter give any id or class to button/
$("input").click(function(){
var val = $(this).val();
$.ajax({url: "path_to_your_cake_php_file.php",
type: 'post', // performing a POST request
data : {
inputValue : val // will be accessible in $_POST['inputValue']
},
dataType: 'json',
success: function(result){
alert('value has been updated');
}});
})
onclick function should have parameter that indicates which answer was submitted.
function save(id)
{
var id = id;
switch(id)
{
case 1:
var buttonValue = "Yes";
break;
case 2:
var buttonValue = "No";
break;
}
var formData = new FormData();
formData.append("ButtonValue", buttonValue);
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
var array = xmlHttp.responseText;
//in array you store results echoed from php script
}
}
xmlHttp.open("POST", "file.php");
xmlHttp.send(formData);
}
file.php
<?php
$buttonValue = $_POST["ButtonValue"];
$con = mysqli_connect("host", "name", "pass", "db");
$res = mysqli_query($con, "Insert goes here");
mysqli_close($con);
?>
PS Consider that pure JavaScript is nearly 30 times faster than JQuery
Use jQuery and jquery-ajax for this.
HTML:
<td> <input type="button" onClick="save(this);" value="Yes">
<input type="button" onClick="save(this);" value="No">
</td>
JS:
function save(elem){
val = $(elem).val() == 'Yes' ? 1 : 0;
$.ajax({
type: "POST",
url: Your_php_file_path,
data: {button: val },
success: function(data){
//added
},
dataType: json
});
}
In the server side you can get submitted value by $_REQUEST['button']

Access on $_POST variables fails on button

I'm keen to know how to send and manage 2 values out of one php-form without reloading/refreshing. This is a simple up-down voting formular to vote with one click.
I've startet with the form:
echo "<form id='votingform' method='post'>";
echo "<button id='rateup' /><i class='fa fa-thumbs-o-up fa-2x'></i></button>";
echo "<button id='ratedown' /><i class='fa fa-thumbs-o-down fa-2x'></i></button>";
echo "<input type='hidden' value='".$id."' name='rateid' />";
echo "</form>";
Then I'm using some jquery to prevent formula-submit and add some visible response for testing purposes:
$(document).ready(function() {
var form = $('#votingform');
var submit = $('#rateup'); // submit button
var submit2 = $('#ratedown'); // submit button
// form submit event
form.on('submit', function(e) {
e.preventDefault(); // prevent default form submit
$.ajax({
url: '', // form action url
type: 'POST', // form submit method get/post
dataType: 'html', // request type html/json/xml
data: form.serialize(), // serialize form data
beforeSend: function() {
alert.fadeOut();
submit.html('Sending....'); // change submit button text
},
success: function(data) {
alert.html(data).fadeIn(); // fade in response data
form.trigger('reset'); // reset form
submit.html('Send Email'); // reset submit button text
},
error: function(e) {
console.log(e)
}
});
});
});
form.on('submit2', function(e) {
e.preventDefault(); // prevent default form submit
$.ajax({
url: '', // form action url
type: 'POST', // form submit method get/post
dataType: 'html', // request type html/json/xml
data: form.serialize(), // serialize form data
beforeSend: function() {
alert.fadeOut();
submit.html('Sending....'); // change submit button text
},
success: function(data) {
alert.html(data).fadeIn(); // fade in response data
form.trigger('reset'); // reset form
submit.html('Send Email'); // reset submit button text
},
error: function(e) {
console.log(e)
}
});
});
});
And added some action:
if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ){
$vote1 = $_POST['rateup'];
$vote2 = $_POST['ratedown'];
echo $vote1; echo $vote2;
// sql part goes here
}
return;
}
The $vote1 is ok, $vote2 is empty. Does anyone have a clue on this and can help me to solve this problem? Or maybe a smarter way to get this thing working? Thanks for your help in advance!
Button elements do not send their value - hence no POSTed value.
Change your elements to
<form id='rateform' action='' method='post'>
<input type='submit' name='submit' class='votecomment' value='up' />
<input type='submit' name='submit' class='votecomment' value='down' />
<input type="hidden" value="1" name="test" />
</form>
Try this :
$http_post = ('POST' == $_SERVER['REQUEST_METHOD']);
if($http_post)
{
echo $_POST['submit']; // no result
echo $_POST['test'];
}
If you click on up then $_POST['submit'] will echo 'up' and if you click on down then $_POST['submit'] will echo 'down'

JQuery's ajax post request does not work as it's supposed to be

The page's link is: localhost/mysite/create-user
This is the code:
<form class="form-horizontal" name = "signUp1F" id = "signUp1F">
<input class="input-xlarge focused" name="pskil" id ="pskil" type="text" placeholder = "Doctor, Trainer, Human Resource etc.">
<input type="hidden" name="neoid" id="neoid" value="<?php echo $neoid; ?>" />
<span><button id = "plus" class="btn btn-success">Plus</button></span>
<div id="skillsAdded"></div>
</form>
The jquery code:
<script type="text/javascript">
$('#plus').click( function(event){
var pskil = $('#pskil').val();
var neoid = $('#neoid').val();
if( !pskil){
alert( "Please write a skill.");
return false;
}
$.ajax({
type: 'post',
url: "localhost/mysite/add-skill",
data: { pskil: pskil, neoid: neoid},
success: function( response){
$('#skillsAdded').append( pskil + "<br>");
return false;
}
});
});
</script>
The purpose is this: user enters a skill value to the input, clicks the Plus button, an ajax request is sent to add the skill to the database. And the code that handles this request is on localhost/mysite/add-skill.
But things go wrong. When I click the "plus" button, it goes to the page localhost/mysite/create-user?pskil=php&neoid=53. What can possibly make this direction? I've been working on this issue for almost 2 hours and I cannot manage to handle it.
The issue is that your button tag submit your form. Here is a updated JavaScript source that you can use. jQuery got a built in preventDefault() method for events. This will for an example prevent the button to submit the form.
<script type="text/javascript">
$('#plus').click( function(event){
event.preventDefault();
var pskil = $('#pskil').val();
var neoid = $('#neoid').val();
if( !pskil){
alert( "Please write a skill.");
return false;
}
$.ajax({
type: 'post',
url: "localhost/mysite/add-skill",
data: { pskil: pskil, neoid: neoid},
success: function( response){
$('#skillsAdded').append( pskil + "<br>");
return false;
}
});
});
</script>
Tryout: http://jsfiddle.net/3A7Mg/1/

Categories