What is wrong with my javascript/jquery code here? - ajax

HTML
<form method="GET">
<input type="text" name="txt"/>
<input type="button" onclick="get()"/>
</form>
returns
Javascript
returns
function get(){
var ik=$('#txt').value();
$.get('action.php',{text:ik});
}
PHP
<?php $text=$_GET['txt']; print $text?>
When I look at firebug I get a notice saying that index txt is
undefined, I tried everything but it's not working. What am I doing
wrong?

$('#txt') will search for an element with id=txt and you don't have any element with id "txt" on your HTML code.
To get a value of the field you should use the .val() method.
The code that should work for you is:
$('input[name="txt"]').val();
Or define an id to your field and your code should be like this:
$('#txt').val();
In your PHP code your are looking for a variable called "txt" and in your Javascript code you are defining "text" as your variable so... you should read the $_GET['text'];
[]'s
Igor.

Where is your #txt element?
Try
<form method="GET">
<input type="text" id="txt" name="txt"/>
<input type="button" onclick="get()"/>
</form>

You are passing {text:ik}, so in your action.php you should get the value you want from $_GET['text'] not $_GET['txt']

Ok, so there are three problems going on.
First, you probably want id=txt in the HTML. You currently have name=txt. The #txt is going to retrieve the ID attribute in the HTML (not the Name attribute).
Here, use this for the HTML:
<form method="GET">
<!-- You need to have an ID value, if you are using #txt in jQuery -->
<!-- I assign both ID and Name to the same value -->
<input type="text" id="txt" name="txt"/>
<input type="button" onclick="get()"/>
</form>
Second, your call to action.php is passing the key "text", not "txt", and third you need to use val().
This might work for the JavaScript:
function get(){
// The #txt is getting an ID value
// You also need to use val() instead of value() ... according to #Igor
var ik=$('#txt').val();
// If PHP is looking for a txt key, you'll want that here as well
// The key could by "mykey" as long as PHP expects "mykey".
$.get('action.php',{txt:ik});
}
So, you'll want to correct both those errors.
The PHP looks fine.

Use the val() method instead off value();
HTML
<input type="button" id="go"/>
Script
$(function(){
var ik=$(input'[name=txt]').val();
$.get('action.php',{txt : ik},function(data){
//do whatever with the response from get call
});
});

Related

put query params inside a filter key laravel

hi i want to change this url from get form
http://127.0.0.1:8000/allfiles?category_id=1&file_id=1
to this in laravel
http://127.0.0.1:8000/allfiles?filter[category_id]=1&filter[file_id]=1
how can i do that?
Actually i want change perquery=? to filter[perquery]=?
thanks
Try something like this in the blade (you can modify it if need):
<form action="http://127.0.0.1:8000/allfiles" method="GET">
<input name="filter['category_id']">
<input name="filter['file_id']">
<input type="submit">
</form>

:value="form.name | slugify" conflicts with v-model on the same element because the latter already expands to a value binding internally

I have a value i want to save to the database as a slug generated from the name. The problem is I cannot use both v-model and :value in the same input field. what is the solution to this? I am using laravel and vuejs. How can i solve this error?
<div class="modal-body">
<div class="form-group">
<label>Slug</label>
<input v-model="form.slug" :value="form.name | slugify" type="text" name="slug"
placeholder="downtown-dubai"
class="form-control" :class="{ 'is-invalid': form.errors.has('slug') }">
<has-error :form="form" field="slug"></has-error>
</div>
</div>
Just generate slug from the name on the backend.
From the Frontend, you only send name and other needed fields, while on the backend you use that name to create a slug.
The easiest and most direct way would by to use v-model="form.name" and get rid of the :value attribute, then just update form.slug using the data from form.name in the function that submits the form. Example:
submitForm() {
this.form.slug = this.$options.filters.slugify(this.form.name)
// Submit the form...
},
If the form.slug field is actually displayed on the page and needs to immediately be reactive, you could also update it using a watcher for form.name like this.
watch: {
'form.name'() {
this.form.slug = this.$options.filters.slugify(this.form.name)
},
},

getelementbyclassname instead of getelementbyid is not working

I have read many times that you can NOW get getElementsByClassName. This below works fine IF I replace ClassName by Id, but using the word ClassName does not work. Anyone know why? (I tried on Chrome and Firefox)
<script type="text/javascript">
function makeDisable(){
var x=document.getElementsByClassName("mySelect");
x.disabled=true
}
function makeEnable(){
var x=document.getElementsByClassName("mySelect");
x.disabled=false
}
</script>
<form>
<select class="mySelect" id="mySelect">
<option>Apple</option>
<option>Banana</option>
<option>Orange</option>
</select>
<input type="button" onclick="makeDisable()" value="Disable list">
<input type="button" onclick="makeEnable()" value="Enable list">
</form>
The function is called getElementsByClassName. Plural. It returns not an element, but an array of all the elements that have the class name.
So even if the array consists of only one item, even there is only one element in the array, you still need to index it.
x[0].disabled=true
instead of
x.disabled=true
Fiddle

Load Dojo form from ajax call

I am trying to implement something like this.
http://app.maqetta.org/mixloginstatic/LoginWindow.html
I want the login page to load but if you click the signup button then an ajax will replace the login form with the signup form.
I have got this to work using this code
dojo.xhrGet({
// The URL of the request
url: "'.$url.'",
// The success callback with result from server
load: function(newContent) {
dojo.byId("'.$contentNode.'").innerHTML = newContent;
},
// The error handler
error: function() {
// Do nothing -- keep old content there
}
});'
the only problem is the new form just loads up as a normal form, not a dojo form. I have tried to return some script with the phaser but it doesnt do anything.
<div id="loginBox"><div class="instructionBox">Please enter your details below and click <a><strong>signup</strong>
</a> to have an activation email sent to you.</div>
<form enctype="application/x-www-form-urlencoded" class="site-form login-form" action="/user/signup" method="post"><div>
<dt id="emailaddress-label"><label for="emailaddress" class="required">Email address</label></dt>
<dd>
<input 0="Errors" id="emailaddress" name="emailaddress" value="" type="text"></dd>
<dt id="password-label"><label for="password" class="required">Password</label></dt>
<dd>
<input 0="Errors" id="password" name="password" value="" type="password"></dd>
<dt id="captcha-input-label"><label for="captcha-input" class="required">Captcha Code</label></dt>
<dd id="captcha-element">
<img width="200" height="50" alt="" src="/captcha/d7849e6f0b95cad032db35e1a853c8f6.png">
<input type="hidden" name="captcha[id]" value="d7849e6f0b95cad032db35e1a853c8f6" id="captcha-id">
<input type="text" name="captcha[input]" id="captcha-input" value="">
<p class="description">Enter the characters shown into the field.</p></dd>
<dt id="submitButton-label"> </dt><dd id="submitButton-element">
<input id="submitButton" name="submitButton" value="Signup" type="submit"></dd>
<dt id="cancelButton-label"> </dt><dd id="cancelButton-element">
<button name="cancelButton" id="cancelButton" type="button">Cancel</button></dd>
</div></form>
<script type="text/javascript">
$(document).ready(function() {
var widget = dijit.byId("signup");
if (widget) {
widget.destroyRecursive(true);
}
dojo.parser.instantiate([dojo.byId("loginBox")]);
dojo.parser.parse(dojo.byId("loginBox"));
});
</script></div>
any advice on how i can get this to load as a dojo form. by the way i am using Zend_Dojo_Form, if i run the code directly then everything works find but through ajax it doesnt work. thanks.
update
I have discovered that if I load the form in my action and run the __toString() on it it works when i load the form from ajax. It must do preparation in __toString()
Firstly; You need to run the dojo parser on html, for it to accept the data-dojo-type (fka dojoType) attributes, like so:
dojo.parser.parse( dojo.byId("'.$contentNode.'") )
This will of course only instantiate dijits where the dojo type is set to something, for instance (for html5 1.7+ syntax) <form data-dojo-type="dijit.form.Form" action="index.php"> ... <button type="submit" data-dojo-type="dijit.form.Button">Send</button> ... </form>.
So you need to change the ajax contents which is set to innerHTML, so that the parser reckognizes the form of the type dijit.form.Form. That said, I urge people into using a complete set of dijit.form.* Elements as input fields.
In regards to:
$(document).ready(function() {});
This function will never get called. The document, youre adding innerHTML to, was ready perhaps a long time a go.
About Zend in this issue:
Youre most likely rendering the above output form from a Zend_ Dojo type form. If the renderer is set as programmatic, you will see above html a script containing a registry for ID=>dojoType mappings. The behavior when inserting <script> as an innerHTML attribute value, the script is not run under most circumstances (!).
You should try something similar to this pseudo for your form controller:
if request is ajax dojoHelper set layout declarative
else dojoHelper set layout programmatic

MVC3 : data retrived from ViewData contains "/"

I have a small problem. I'm passing a id from one view to another.
I do it like this:
#Html.Partial("DetailViews/_Parameters", Model.Parameters, new ViewDataDictionary { { "data-resourceId", Model.Id } })
I use this id for a hidden field in a form on the partial view. like this:
<form id="formAddNewParameter" action"#" title="Add new Parameter">
<input type="hidden" name="resourceId" id="resourceId" class="required" rel="0" value=#ViewData["data-resourceId"]/>
</form>
The ID is passed as it should. BUT there is always a "/" at the end...
Anyone know where this "/" comes from.
You are missing the qutation marks "" around the value tag's value
<form id="formAddNewParameter" action"#" title="Add new Parameter">
<input type="hidden" name="resourceId"
id="resourceId" class="required" rel="0"
value="#(ViewData["data-resourceId"])" />
</form>
And I guess the tag closing / somehow mixed into the value tag.

Resources