Search Button Broken on Magento - javascript

First off, I am not a developer. We don't have a dedicated developer for our Magento website. A few days ago my boss asked me to fix an error on the website. What is happening is the search button doesn't work. I try to click it and nothing happens. When I go into Inspect Element, there is an error in the console. It says "Uncaught TypeError: Cannot read property 'get' of undefined" in prototype.js:5557.
I have tried everything I can think of to fix it. Which to be honest isn't a lot. I've reindexed files, cleared the cache, updated Magento, and made sure the button is located where its supposed to be. I've made sure products are searchable within Magento and I have made sure the search type is set to like/fulltext. I think its probably an error in the code, but I have no idea what to edit or where. I am going mad trying to figure this out.
The link to the store is: http://store.excitementvideo.net/ But please keep in mind it is NSFW.
Thank you
Update: I've been told its a form error? Here is my form.mini.phtml file
<form id="search_mini_form" action="<?php echo $this->helper('catalogsearch')->getResultUrl() ?>" method="get">
<div class="form-search">
<label for="search"><?php echo $this->__('Search:') ?></label>
<form> <input id="search" type="text" name="<?php echo $this->helper('catalogsearch')->getQueryParamName() ?>" value="<?php echo $this->helper('catalogsearch')->getEscapedQueryText() ?>" class="input-text" /></form>
<form><button type="submit" title="<?php echo $this->__('Search') ?>" class="button"><span><span><?php echo $this->__('Search') ?></span></span></button></form>
<div id="search_autocomplete" class="search-autocomplete"></div>
<script type="text/javascript">
//<![CDATA[
var searchForm = new Varien.searchForm('search_mini_form', 'search', '<?php echo $this->__('Search entire store here...') ?>');
searchForm.initAutocomplete('<?php echo $this->helper('catalogsearch')->getSuggestUrl() ?>', 'search_autocomplete');
//]]>
</script>
</div>
</form>

Looks like the javascript has slight bug with the string part.
try changing
<script type="text/javascript">
//<![CDATA[
var searchForm = new Varien.searchForm('search_mini_form', 'search', '<?php echo $this->__('Search entire store here...') ?>');
searchForm.initAutocomplete('<?php echo $this->helper('catalogsearch')->getSuggestUrl() ?>', 'search_autocomplete');
//]]>
</script>
to
<script type="text/javascript">
//<![CDATA[
var searchForm = new Varien.searchForm('search_mini_form', 'search', '<?php echo $this->__("Search entire store here...") ?>');
searchForm.initAutocomplete('<?php echo $this->helper("catalogsearch")->getSuggestUrl() ?>', 'search_autocomplete');
//]]>
</script>

Related

Pass PHP Date variable to Javascript Function

I have a php date created with the code:
$date = date("d-m-Y");
I can echo this date out with:
echo $date;
and that works fine. But I want to pass this to a javascript function onClick of a button on my page. So I have:
<input type="button" onClick="myFunction(<?php echo $date; ?>)" value="Today">
Pretty standard. But when I alert the function in javascript:
function myFunction(phpDate) {
alert(phpDate);
}
it then gives me:
-2018
in an alert box.
Full code
For anyone wondering, here is my full code:
<?php
$date = date(d-m-Y);
echo $date; // to test date is working (it is)
?>
<script type="text/javascript">
function myFunction(phpDate) {
alert(phpDate);
document.getElementByID("dateField").valueAsDate = phpDate;
// can someone please tell me if ^^^this^^^ line above is correct syntax wise.
// I'm particularly concerned with '.valueAsDate'.
}
</script>
<html>
<input type="date" id="dateField">
<input type="button" onClick="myFunction(<?php echo $date; ?>)" value="Today">
</html>
BTW I do have <!doctype html> and <head> and <body> tags in there. So my page works.
You are actually passing the date as Number (2 - 1 - 2019 = -2018 for example), not as a String. You will need to wrap the date value in single quotes
<input type="button" onClick="myFunction('<?php echo $date; ?>')" value="Today">

Get Php variable in to Javascript function

I know this topic is frequently asked but I've tried many ways and nothing works for me. I have a lot of checkboxes with the id generated (in Php) by a variable like this:<input type="checkbox" id="<?php echo $id; ?>" >. That I want to do is call a Javascript function every time that a radiobutton (autogenerated) is clicked for that that checkbox with id="<?php echo $id; ?>"can be checked.
I tried with things like:
function check() {
var id = '<?php echo $id; ?>';
document.getElementById(id).checked = true;
}
I know that maybe I could do with Ajax but I don't know how.
(Up to this point is obvious that you know that the radiobutton has onclick = "check ()").
If anyone can tell me what I can do is I appreciate a lot.
You can try something like this:
<input type="checkbox" id="checkbox-<?php echo $id; ?>" />
<input type="radio" data-check="checkbox-<?php echo $id; ?>" onclick="check(this)" />
<script>
function check(e)
{
document.getElementById(e.getAttribute('data-check')).checked = true;
}
</script>
You can use below code for dynamically generated radio and checkbox
$("input[type='radio']").click(function() {
$(this).closest('input[type="checkbox"]').prop('checked', true);
});

Javascript innerHTML with session PHP from other page

I have 2 page
the innerhtml onclick page (a.php)
the page that start session (b.php)
in a.php I write function onclick to change some text
and I add the session from b.php in this text, but nothing change in text
$(document).ready(function(){
$('.add-to-cart').on('click',function(){
document.getElementById('outside_cart_text').innerHTML =
"Qty type <?php echo $this->session->userdata('qty_type'); ?> amount <?php echo $this->session->userdata('qty_product');?>";
});
});
it change the original text to "Qty type amount". But session value not appear.
the question is how to make it appear instantly?
additional detail : My click is on the button sumbit to other page, but I have already use this trick. It work like ajax. so after click I still in the same page (and not reload)
<style>
.hide { position:absolute; top:-1px; left:-1px; width:1px; height:1px; }
</style>
<iframe name="hiddenFrame" class="hide"></iframe>
<form action="receiver.pl" method="post" target="hiddenFrame">
<input name="signed" type="checkbox">
<input value="Save" type="submit">
</form>
Maybe it would work if the variables are encoded in json format.
document.getElementById('outside_cart_text').innerHTML =
"Qty type
<?php echo json_encode($this->session->userdata('qty_type')); ?>
amount
<?php echo json_encode($this->session->userdata('qty_product'));?>";
});
$(document).ready(function(){
$('.add-to-cart').on('click',function(){
var qty_type = '<?php echo $this->session->userdata('qty_type'); ?>';
var amount = '<?php echo $this->session->userdata('qty_product'); ?>';
$("#outside_cart_text").html("Qty type "+qty_type+" amount "+amount);
});
});

send php post variable to url using javascript when button is clicked

I have a php page which gets $page and $user using post method, I also have a button that i want, to open a URL in the same window using $page and $user variables when clicked, to use them with $_GET[] function.
i want my URL be like:
http://www.test.com/test.php?page=$page&user=$user
my code is like this:
<?php
$page=$POST_['page'];
$user=$POST_['user'];
<?
<html>
<head>
function openurl() {
var user=<?php echo "$user";?>;
var page=<?php echo "$page";?>;
open('www.test.com/test.php?page='+page'&user='+user,'_self');
}
</head>
<body>
<button onclick="openurl()" type="button">open url</button>
</body>
</html>
There is no need for scripting at all
If you want GET:
<?php
$page=$GET_['page']; // should be sanitized and you can use REQUEST for either
$user=$GET_['user'];
$parm = "page=".$page."&user=".$user;
?>
Open URL
If you need to post:
<form action="test.php" method="post">
<input type="hidden" name="page" value="<?php echo $page; ?>"/>
<input type="hidden" name="user" value="<?php echo $user; ?>"/>
<input type="submit" value="Open URL" />
</form>
Change these lines:
<?php
$page=$POST_['page'];
$user=$POST_['user'];
<?
....
var user=<?php echo "$user";?>;
var page=<?php echo "$page";?>;
open('www.test.com/test.php?page='+page'&user='+user,'_self');
to this:
<?php
$page=$_POST['page']; //incorrect $_POST declaration
$user=$_POST['user']; //incorrect $_POST declaration
?> //php tag incorrectly closed
....
var user=<?php echo $user;?>; //echoing a variable not string (no need for quotes)
var page=<?php echo $page;?>; // echoing a variable not string (no need for quotes)
open('www.test.com/test.php?page='+page+'&user='+user,'_self'); // link was broken, forget to put '+' after page variable in link.
You can create a form and via Javascript just run YOURFORMNAME.submit();
Use href in javaScript to move to another location:
location.href="www.test.com/test.php?page='+page'&user='+user"

How to submit one form out of multiple with javascript

On my website I have a big list of all the letters people can view.
For every letter there is a form which sends some data from the letter to the next page. What I want to do is to submit the form with a link, and not with a button.
As far as I know, that is not possible with PHP. So i tried making what i want in Javascript.
My programming experience in Javascript is litterly 0%. I need some help with this :)
This is the code I'm using:
<script type="text/javascript">
function submitform()
{
document.forms["brief_weergeven"].submit();
}
</script>
<form id='brief_weergeven[]' action='<?php echo $main; ?>beveiligd/achterbanner/nieuwsbrief.php' method='POST'>
<input type='hidden' name='nummer' value='<?php echo $brieven[0]; ?>'>
<input type='hidden' name='datum_maand' value='<?php echo $maand_getal; ?>'>
<input type='hidden' name='datum_jaar' value='<?php echo $jaar; ?>'>
<a href="javascript: submitform()" class='button'>Nieuwsbrief <?php echo $brieven[0]; ?> (<?php echo $maand .' ' .$jaar; ?>)<BR></a>
</form>
This code runs for every letter. As you can see, I'm using a array to give all the forms a seperate ID. The only thing is that i have no idea how to let javascript know which forum i submitted.
How can I do this? Please remember that i know nothing about javascript, so please explain what you're doing in your answer.
You dont need javascript to do this, just use css to make your button look like a link:
Css:
button {
border:none;
padding:0;
background: none;
color: blue;
text-decoration: underline;
cursor: pointer;
}
html:
<button type="submit">Submit</button>
You can usedocument.getElementById('form-id')
And event handler like this
var form = document.getElementById('your-form-id');
form.onsubmit = function(){
// code
}
user574632 gave you a nice alternative. Still even want to use JavaScript you can try below.
Use the jQuery onclick method having the id attribute of the link as selector.
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#submit_link").on("click", function(){
var form = $(this);
var url = form.attr("action");
var data = form.serialize();
$.post(url, data);
});
});
</script>
<form id='brief_weergeven[]' action='<?php echo $main; ?>beveiligd/achterbanner/nieuwsbrief.php' method='POST'>
<input type='hidden' name='nummer' value='<?php echo $brieven[0]; ?>'>
<input type='hidden' name='datum_maand' value='<?php echo $maand_getal; ?>'>
<input type='hidden' name='datum_jaar' value='<?php echo $jaar; ?>'>
<a href="#" id="submit_link" class='button'>Nieuwsbrief <?php echo $brieven[0]; ?> (<?php echo $maand .' ' .$jaar; ?>)<BR></a>
</form>

Categories

Resources