Get text with id of textbox javascript - php

I know, this is just so common. However, I am not able to get it working.
Got a php file which is included in the index.php and that contains a function printing this one out
echo "<input type=text id=test name=schuelername value=Name><br>
<button style='float: left;' onclick='update();'>Suchen</button>";
Now I want to pass this value of the textbox. With (this is the update function)
alert(window.document.getElementById("test").value);
run by an external JS file (that works, Tested it, the only problem is the value of the textbox) isn't showing up.
Argh. Why do I define the damn ID if JS can't access it (or so...)
SOLVED: Well, I ran a document.write before it and I couldn't imagine, that like this, the rest of the javascript can't run because I just deleted everything. Thanks alot, stupid me, late!

That should work. Try alert(window.document.getElementById("test").defaultValue);
Demo: http://jsfiddle.net/AlienWebguy/RzrZZ/
Make sure your PHP is echoing that HTML before your Javascript tries to find it and alert the value. If the actions are reversed, getElementById("test") will be undefined because the node would not have been added to the DOM yet.

Related

Get data from HTML and save it as a PHP variable

I have started learning php and I have a question.Let's say I have the following html code:
<p id='tobeChanged'>I wil be changed throughout the execution<p>
This paragraph is not static.Its content can be changed from the user with a button which will produce a random number and will replace the paragraphs html.
E.g. from
p id='tobeChanged'>I wil be changed throughout the execution<p>
to
<p id='tobeChanged'>42<p><!--changed with a button-->
Now my question.Is it possible to pass the new produced value to a php variable?If possible i would like a long explanation.
Also i would like not to use forms(if possible).
Thanks In advance
You need to fire an AJAX request on that button click, that will send that value to server making php to read it.
You can do something like this (you need to include jQuery on page):
$.post("/saveVariable.php",{randNum:randomNum},function(data){alert("Data saved successfully");})
At PHP end, you will get the value in
$_POST['randNum']
Maybe that will help.

Make value=null when page first loads

Is it possible to remove a value whilst the page is loading?
I'm using OSClass, and on one of the pages it's by default adding a value for region (cambridshire):
I need to clear this value since it's causing problems, everytime I type something else in, by default, it registers it as Cambridgeshire...
If I look at code for it:
It's being generated by a function (can see my JQuery attempt to clear it which hasn't worked).
Then if I search the function is splits up in many different parts, so I don't know where to go from there.
Basically, is there a way to remove the value when page loads and save the new value when the user submits?
JSFiddle - Note it won't display anything due to the way code is generated
Have you tried removing <?php ItemForm::region_text(); ?> on it's own? Or passing an empty string <?php ItemForm::region_text(''); ?>
I'm not familiar with OSClass though I'm afraid.
Paste this code below the jQuery initialization:
$(document).ready(function(){
$("#region").val('');
})

Pass a PHP variable in a link to a Modal box

Hi i have a html link to a modal box which works perfectly, now what I am trying to do is pass a variable to the form in the modal box, how ever the link shows but nothing happens once it is clicked.
Here is my normal code:
Add
Here is my PHP code
echo "<a href=\"#accSettings1?ip_address={$ip_address}\" class='btn btn-small btn-primary hidden-tablet hidden-phone' data-toggle='modal' data-original-title=''>Add</a>";
Use your normal code and add PHP only when you need a PHP variable:
Add
if it's your application that doesn't work - then debug it first.
Use handwritten example to exercise with. Make it work. And then add a dynamical part using PHP to fill the variable. After that you have to verify if dynamical code produced the same result as a static one. To do that instead of hover you have to inspect page source and compare it with original code. Find the differences and correct them.
To me, such an url like #accSettings1?ip_address=value looks quite unusual. ?ip_address=value#accSettings1 looks more familiar to me. Though I am not a JS pro, nor I know your app internals and URLs intention

My PHP variable keeps echo'ing same info - not the new data in URL to grab

I am having a problem where I am successfully setting a URL variable in the form of mywebsite.com/contact/?product=VAR1 from one page to another, and it will be read on the 2nd page with my simple code.
First use is fine but any thing after that is not. There is a problem with the variable not being cleared/re-read/reset. The URL Bar even reads fine in the URL bar mywebsite.com/contact/?product=VAR2 so that first part works.
But the code doesn't want to get the VAR2 part once it has stored VAR1. It just keeps re-displaying VAR1 once it is loaded once.
<?php echo htmlentities($_GET['product']); ?>
If it makes a difference, I am succesfully displaying the ?product= information inside of a jQuery value changer with this:
$(".input-text").val('<?php echo htmlentities($_GET['product']); ?>');
I have tried my own solutions like putting in a unset($product); before the last ?> but no avail.
I have a limited knowledge of PHP/jQuery and would like to use this way of setting/grabbing variable since it is simple. I am using it in jQuery because it can re-write the exact form input-box value with this variable passed along, and successfully send it in my CMS's contact form.
------------------------ updated: added code ------------------------
(I am just displaying all the code logically as I can think of)
#1: Set up the variable into the URL. The first echo getURL displays the url, I add my ?product= and then it gets the item name. This is all fine.
<a href="<?php echo Mage::getURL('webforms/index/index/id/2') ?>?product=<?php echo $_product->getName(); ?>">
#2: My form is set up within my CMS. It has a special class so I can select it exactly within my jQuery. I do not have access to all of this, it is generated by the CMS. The purpose obviously is to get selected products to be rememmberd via this Variable, and serve purpose in the contact form.
<div class="field webforms-fields-enquiryfield">
<label for="field_20">
Product in Enquiry
</label>
<div class="input-box">
<input type="text" name="field[20]" id="field[20]" class="input-text" style="" value="">
</div>
</div>
#3: Now that my area to display, & variable are set up, we use jQuery to insert the variable into the actual value of the input-text form. I am doing this because within my CMS I am not able to simply add it in. I have to use jQuery to replace the text (which is empty box anyway).
$(".webforms-fields-enquiryfield .input-text").val('<?php echo htmlentities($_GET['product']); ?>');
Hope that made more sense.
I have figured the answer to this, the problem was that the CMS Magento likes to cache certain blocks - which is why I could view it first, then my CMS would store the variable but not erase it. No matter what kind of fixes I tried to disable caching, no go.
Instead I ended up using the following code, using a # variable, and some simple JavaScript that anyone can understand. I will be using this in future projects no doubt! Very easy variable passing ...
if(window.location.hash) {
var hash_value = window.location.hash.replace('#', '');
$("my-css-input-field-or-whatever-you-want").val(hash_value);
}
The hash_value can be used in a variety of ways now I suppose, and this works well around my Magento set up, it does not get cached/stored. Grabbing the URL location & hash instead.
(And the PHP used, simplified for anyone to replace/reuse within a CMS)
<a href="[GetBaseURLfromCMS]#[MyDynamicVariableHere] ">

How do i use this javascript variable properly?

I am trying to expand on some code written by someone else, but I am having trouble using one of the javascript variables. If I set it in the title of a div or something similar like so:
$("#test12").attr("title" , ccode);
then it works fine and the title of the div is 'CA', which it should be. But if I try to put it into the text area of the div, then it tries to run a function or something but I can't see where it's doing it.
Is there a way I can convert it to simple text and stop it from running any functions?
Thanks for any help
Edit:
This is all of the code I can see at the moment:
<script>
//<!--
function loadForeignOffices(ccode){
//load window with details...
$('#iframe_3').attr("src", "<?php echo $html->url("/", true); ?>erc/maps/contacts/"+ccode);
$("#test12").attr("title" , ccode);
}
//-->
</script>
Basically what I'm trying to do is use the ccode variable because I want to display CA on the screen, but when I try to do that it seems to run some other function and fails, and doesnt show CA.
You may need to brush up on your jQuery dot-operators:
.load() is an ajax call that will load the contents of () into the selected element. In one of your comments you mention you are trying to do this $("#test12").load(ccode); which is inherently silly, if ccode is the string "CA"
.attr() will change the attributes of the selected element. If you are trying to display "CA", $("#test12").attr("title" , ccode); this will clearly not accomplish that -- As "CA" is added to the title attribute. As in <div id="test12" title="CA"></div>
Perhaps what you are looking for is .html() which inserts the string into the selected element. Or even .innerText()
Why don't you spend some time reading the jQuery documentation. A little bit goes a long way!
Keep in mind your PHP code is executing first on the server, then your javascript.
Here's a JS Fiddle as a start: http://jsfiddle.net/QsFJ4/3/

Categories