sorry if I am missing something simple here. I am using CakePHP 3 and the Friendsofcake Search plug-in and trying to load my results with AJAX. I am not sure what to set for the URL - my understanding is the FormHelper url and the AJAX url must match for SecurityComponent. However even with that disabled I cannot get the form to submit. Any help is appreciated. The plugin is working fine otherwise and I can submit other forms using AJAX just fine - I suspect I am missing something here (or it's not possible - I am a beginning programmer)
<?php
$formUrl='//'.$_SERVER['HTTP_HOST'].Router::url(['controller'=>'Treasures','action'=>'frontIndex']);
echo $this->Form->create('Treasure',['id'=>'myForm','url'=>$formUrl]);
echo $this->Form->input('q');
... (Form submit, end, etc.)
?>
<script>
$( "#myForm" ).submit(function( event ) {
event.preventDefault();
$.ajax({
async:true,
data:$(this).serialize(),
dataType:"html",
success:function (data, textStatus) {
$(".ajax-result").html(data);
},
type:"POST",
url:"<?=$formUrl?>"
});
</script>
<div class="ajax-result"></div>
Can someone tell me what I should be setting for the $formUrl? Currently the Controller action I am using this on successfully filters data with the search plugin and I have specialized the view to return AJAX results when requested - but there is obviously something else going on I am missing.
This works if I leave the URL blank and use GET instead of POST - should've thought of that sooner.
echo $this->Form->create('Treasure',['id'=>'myForm']);
...
?>
<script>
$( "#myForm" ).submit(function( event ) {
event.preventDefault();
$.ajax({
async:true,
data:$(this).serialize(),
dataType:"html",
success:function (data, textStatus) {
$(".ajax-result").html(data);
},
type:"GET"
});
</script>
Works as expected. Hope this helps someone!
Related
I have searched and red a lot of this issue.
However, I do not know, why this few simple lines produce a
"POST http://example.com/wp-admin/admin-ajax.php 400 (Bad Request)"
What I basically want (later on) is: If you press a button, a counter will be updated and write a value into my mySQL. But before I can think about a mySQL query, I stumble over this 400 Error.
What I have (really basic, no validations, etc.):
html
<button id="vote" type="button">Click Me</button>
jQuery
<script type="text/javascript">
//var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>"; // get ajaxurl
jQuery('#vote').click(function() {
jQuery.ajax({
url: 'http://example.com/wp-admin/admin-ajax.php', //just hard coded to see if I have a problem with my ajaxurl
type : 'post',
data: {
'action' : 'ibvote'
},
success: function( data) {
alert( data);}
});
});
</script>
php
add_action("wp_ajax_ibvote", "ibvote");
add_action("wp_ajax_nopriv_ibvote", "ibvote");
function ibvote(){
echo "DONE";
wp_die();
}
Any help would be great.
Thank you.
Thank you #all.
I use the "Code Snippets" plugin and I activated "only run on site frontend". Today with more coffee I switched to run snippet everywhere and: voila! Working like a charm.
#Bhautik: Again thank you for your time and answer :)
My ajax code didn't work, i am trying to pass the colorit value from my script to my controller(php), but the ajax didnt work. the alert box with out put 'success!' didn't pop out and also the alert(colorit). but if i comment the ajax code, the alert(colorit) pops. is there any wrong in my ajax code? please help. tnx. sorry im new to this.
script
$( ".colorselector_1" ).change(function() {
var colorit = document.getElementById("colorselector_1").value;
alert(colorit);
$.ajax({
url: '/addItemColor',
type: 'GET',
data: {'colorit':colorit},
success:function(data){
alert('success!');
}
});
});
html
<select id="colorselector_1" class="colorselector_1">
<option value="#A0522D" data-color="#A0522D">sienna</option>
<option value="#CD5C5C" data-color="#CD5C5C" selected="selected">indianred</option>
</select>
route
Route::get('addItemColor','CakeController#addItemColor');
controller
public function addItemColor(){
.......}
May I just suggest something?
First of all, this is why you have vars like "error" in place. So the first thing you should do is change your code accordingly:
$( ".colorselector_1" ).change(function() {
var colorit = document.getElementById("colorselector_1").value;
alert(colorit);
$.ajax({
url: '/addItemColor',
type: 'GET',
data: 'colorit':colorit,
success:function(data){
alert('success!')},
error:function(data){
console.log(data);
alert('error');
}
});
});
Another thing to remember is that you have the Network panel in developer tools where you can see all of the calls that were made and whether they had successful responses or not, such as in Chrome when you go to More tools -> Developer tools. These two will help you find the answer of "whether there is something" for yourself, like if there's a wrong URL or something.
Make sure you point to the right url in your $.ajax function.
well after reading all the related topics still no success,
I have 4 files index.php with a simple form, after submit I use process.php to send back (ajax) errors to index.php using external script.js file and also send mail to the owner of this site,all i need is that the user will also be redirected to a thank-you.html page (if there are no errors of course) but no luck ,I have tried all the combinations suggested:
header("Location: http://www.mywebsite.com/thank-you.html");
header("Location:thank-you.html");
if (success).....
echo
<script type="text/javascript">
<!--
window.location = "http://www.website.com/thank-you.html";
//-->
</script>
I have tried to put it in the bottom of the process,php ,also in the top of the page also tried to put in the script.js-inside $ajax function but nothing:(((
Can anyone tell me what to do?
solved:
thank you all so much:))))) it should be inside the ajax function in the script , right after success: function(data){ I have placed it in the bottom of the script and it didnt work before but now its perfect!
If you're doing this inside of a jQuery $.ajax() call, use the "success" method instead.
$.ajax({
type: "post",
//etc...
success: function(){
window.location.href="thank-you.html"
}
});
The correct usage in JS is "window.location.href".
Also, a PHP file with "header" being set during an AJAX call won't redirect the client browser.
If I understand correctly you will need to handle the redirect once you get the response from process.php IE: in the success callback.
$.ajax({
success: function () {
window.location.href = 'thank-you.html';
}
});
echo '
<script type="text/javascript">
<!--
window.location = "http://www.website.com/thank-you.html";
//-->
</script>';
assuming that is from php you were missing quotes.
I handled it a little differently as I handled something similar recently. I have the PHP page echo back "true" (this could also just be an INT) and then run if/else statements in the ajax.
$.ajax({
url : "process.php",
type: "POST",
data : formData,
success: function(data, textStatus, jqXHR)
{
if (data == "true")
{
window.location = "http://www.website.com/thank-you.html";}
}
I have a form in a jquerymobile dialog box that I am submitting via jQuery Ajax.
Currently my problem is that once the form is submitted the same dialog box is opened again on top of the original dialogbox.
So that my url reads before submission:
url/index.php#&ui-state=dialog
and then after submission:
url/index.php#&ui-state=dialog#&ui-state=dialog&ui-state=dialog
Has anyone ever encountered something like this before?
[edit added code example]
$(function(){
$("#form").submit(function(e){
e.preventDefault();
var dataString = $("#form").serialize();
errorInput = $("input[name=valOne]#valOne").val();
$.ajax({
type: "GET",
url: "formHandler.php",
data: dataString,
dataType: "text",
success: function(data){
if(data.toLowerCase().indexOf("error") >= 0){
alert(data);
$(".ui-dialog").dialog("close");
$("#valOne").val(errorInput); //the reentering info so user doesn't have to
}else{
$(".ui-dialog").dialog("close");
location.href="index.php";
}
},
error:function (xhr, ajaxOptions, thrownError){
alert(thrownError);
}
});
});
});
You can set your own handler on form with this submit
Use two forms for page and for your dialog window.
Have you tried using $.mobile.changePage("url here") instead of location.href?
More details here http://jquerymobile.com/test/docs/api/methods.html
Would it not be easier to just refresh the page with JS instead of loading it again? It might be calling the dialog functions twice.
I had similar problem with forms. I decided to use <div data-role="fieldcontain"> instead. Now it works good with no "refresh effect". In this case you shouldmake your own message instead of .serialize.
I am using CJuidialog widget to wrap a view file, and I don't want the default 'save' button because I would like to use a javascript to make an ajax call to the server for data validation, then save it. I tried below:
<?php
if($model->isNewRecord)
echo CHtml::submitButton('Create');
else
echo '<button onClick="javascript: _updatedata('.$model->id.');">Save</button>';
?>
when the save button is clicked. it still will go to the actionUpdate to save the form data, however I have created an action to just save my data.
function updatedata(id)
{
var url = '<?php echo Yii::app()->request->baseUrl ?>' + '/index.php?r=user/profileupdate&id='+id;
......
$.ajax({
url: url,
type: 'POST',
dataType: "html",
data:
{
...
},
success: function(data, textStatus, XMLHttpRequest) {
if (data != null && data == "success")
{
//$('#xccdfgrid').trigger('reloadGrid');
$('#userprofile').dialog('close');
}
else
alert(data);
},
......
If you got your question correct then what you are saying that instead of default button you need something that you can perform Ajax Things rite? if yes then you need to have Ajax Submit Button. Yii gives you liberty to use ajax more easily then you are trying to do. Please see CJuiDialog and AjaxSubmitButton