Problems with <form> and Dynamically-generated links - php

Related directly to this post, I am having trouble implementing some sound advice given from #sean, because as you can see on this page:
http://www.onestopfasteners.com.au/checkout.php - I have wrapped the form tags around the table element, but the form just doesn't seem to be working, and nothing ever gets "POST"ed either. I've changed the code around abit to experiment, but haven't found a solution, and no search anywhere has proven to be useful yet.
I'd appreciate any help at all. I'm completely baffled!
Thanks!
P.S. I thought that maybe the fact that I am wrapping form elements around dynamically generated content could be why the form isn't working, but that doesn't make much sense to me and, I've done it before, so that can't be it, can it?
Code:
I know, it's long, apologies in advance. :)
<?php
// (c) code removed ;) problem solved - thanks to everyone who helped!
?>

I think your problem is with:
function submit() {
document.myform.submit();
}
Try:
function submit() {
document.getElementById('ct_form').submit();
}
It looks like you are using jQuery in the page so you could also use:
function submit() {
$('#ct_form').submit();
}

Your using javascript to submit the form, but you are referencing document.myform which doesn't exsist.
try this instead.
document.getElementById('ct_form').submit()

// do sumbit first form of document
document.forms[0].submit()
document.getElementById is not necessary here. document.myform relies on NAME attribute of FORM element, by the way

Related

New element on a php array file

I'm a total noob on php, but a friend of mine asked for help and I thought I might do it..
I have this code/file here and want to find a way to add an element in this array via html file. I know it sounds noob, it does for me too, but please help, I've seen arrays, vectors and lists only on c++, tried to take a look at the documentation of php5 (since he want it in php 5) but I couldn't make it!
here it is...
<?php
$bledi = array('user12345', 'user2016', 'user5749852', 'user985658', 'HowToAddANewElement');
echo $result;
?>
You need a form with a text input in HTML and a send button.
When the form is submitted to the action page there you could put
your php.
You can make a global array ($bledi) and you will have an if condition there that says something like
if($_REQUEST['input_text'])
$bledi[] = $_REQUEST['input_text'];

Location.reload more elegant/efficient solution

Situation:
I have an html page with a PHP function on it. The function echoes the elements of an array in a foreach loop. The data of the array is used in each individual echo.
foreach ($DB->query($sql) as $v) {
echo $v['specificData'];
}
All new elements are added to the array with ajax.
Problem:
I have been using location.reload to refresh the page each time a new element is added so it show ups right away. The problem is that that solution isn't elegant/efficient at all.
Question:
What are my other options? The easiest, the better.
Appreciate any help and advice ;)
P.S: I already thought about jquery.append() but I'm hoping someone has an easier idea xD
Refresh just the part of the page that needs refreshing. You can make it smooth by doing something like this in jQuery upon successful AJAX callback:
$(element).fadeOut(function(){
$(element).html(ajax_response_here).fadeIn(); // .html() or .append() whichever makes more sense
});
If you already using ajax to send the data back to the server its should be hard to use it to update the dom as well. If you want an efficient way then probably this is the way to go. If you want to avoid jQ for updating the site, you can simply write a basic js function.
function appendTo(parent, content) {
parent.innerHTML += "<p>"+content+"</p>";
}
or
function appendToById(parentId, content) {
appendTo(document.getElementById(parentId), content);
}

Codeigniter PHP - loading a view at an anchor point

I have a form at the bottom of a long page, if a user fills out the form but it doesn't validate the page is reloaded in the typical codeigniter fashion:
$this->load->view('template',$data);
however because the form is way down at the bottom of the page I need the page to load down there like you do with HTML anchors. Does anyone know how to do this in codeigniter?
I can't use the codeigniter
redirect();
function because it loses the object and the validation errors are gone. Other frameworks I've used like Yii you can call the redirect function like:
$this->redirect();
which solves the problem because you keep the object. I've tried using:
$this->index()
within the controller which works fine as a redirect but the validation errors are in another method which is where the current page is loaded from:
$this->item($labs)
but when I use this it get stuck in a loop
Any ideas? I've seen this question a lot on the net but no clear answers. I'm researching using codeigniter "flash data" but think it's a bit overkill.
cheers.
I can't personally vouch for this, but according to this thread if you append the anchor to the form's action, it will work.
CodeIgniter helper:
<?php echo form_open('controller/function#anchor'); ?>
Or vanilla HTML:
<form method='post' action='controller/function#anchor'>
If you were open to using Javascript, you could easily detect a $validation_failed variable and appropriately scroll. Or, even better, use AJAX.
Another option is to put the form near the top of the page?
Ok, as far as I understood your problem, it isn't much related to the back end(codeigniter). You want the form at the bottom of the page to be 'what-users-sees-on-page-load' (since you mention anchors).
Now, what you can do is, you can set delimiters for your validation error messages using:
echo validation_errors('<div id="bottom_form_error">', '</div>');
Using jQuery ScrollTo, do:
$( function() { $('#bottom_form_error').ScrollTo(); } );
And, the user will be scrolled to the errors at the bottom of the page. Don't forget to include jQuery too.
Anchor hash fragment click is different - it is scrolling at ∞ speed.
I hope that is what you wanted.
P.S. I am ignoring what you said below this line:
Does anyone know how to do this in codeigniter?
as I felt it is not really relevant to the question.

JavaScript functions give errors when loaded via Ajax

EDIT:
OK, I believe I've found a way around the issue using the info posted by #ManseUK along with #Johan's comment. As a n00b I can't answer my own question but I've added an explanation below the question in case it helps anyone else out.
I am re-writing part of an e-commerce solution which was written by
another development team some years ago. In the new version, we are
experimenting with shortening the user journey using Ajax but doing so
gives JavaScript errors and causes some functions to fail. Dev URL is
here:
http://cognition.thelightbulb.co.uk/type-18/general-purpose-lamps-and-bulbs/craftlight-daylight
The errors appear once the dropdowns have been selected and the
product displays.
The errors are displaying most notably in IE7:
Error: 'frm.qty' is null or not an object
Error: 'qty.value' is null or not an object
I believe this is where the problem is coming from:
var frm = document.frmOrder;
var qty = frm.qty;
In the lines above, frmOrder is the name of the form and qty is
the name of the input for product quantity.
Compare that to http://cognition.thelightbulb.co.uk/product-54 where
the product loads without the Ajax selection process and you'll see
that the functions work correctly.
I suspect that the problem is to do with the fact that var frm =
document.frmOrder; is not working due to the way it relates to the
DOM when loaded with Ajax.
I am using innerHTML=xmlhttp.responseText as the Ajax method. Is
there an alternative way to define var frm so that it will function
properly when loaded with Ajax?
EDIT:
Using the info posted by #ManseUK along with #Johan's comment, I added another argument to CheckMinQty(minorder) so that it now looks like this...
function CheckMinQty(minorder,qty)
...where qty is passed to the function on an onclick event as document.forms['frmOrder'].qty.value
I then moved the whole function out into a separate .js file. It's maybe not the best approach but it still feels tidier for the Ajax call to just return workable HTML which CheckMinQty can use rather than bringing in a whole load of <script> and then trying to run it.
Thanks for all the suggestions and I'd welcome any comments about the approach/solution outlined above.
Change this
var frm = document.frmOrder;
to this
var frm = document.forms['frmOrder'];
That will give you a handle to the form
document.frmOrder refers to the element with id frmOrder on the page, which happens to be the form on this page. Just try to get the correct form-element as the variable there.
Though the Manse's solution might work, use a more sensible way and assign an id to the form and since you're using jQuery anyway, retrieve the form with var frm = $(#formid); Not only is it easier to write, it's much more easier to read by you and everybody else.
When loading script via AJAX, you don't have DOMReady event anymore. In other words, when you want to execute your script on AJAX load, you should use self-invoked functions.
Wrap your ajax-loaded script inside a function like this:
(function(){
// Do what you want to do here.
})();
See if that solves the problem?

update a form and not load or reload page

I have a simple command:
<?php echo form_open("sales/add",array('id'=>'add_item_form')); ?>
How can I make this command so it just updates the form, without reload or redirection. I dont even need to see the form. I have tried all sorts of methods, but wondering if there is something simple here I am missing. I just need to post the data.
This is what is outputted by codeigniter.
<form action="http://127.0.0.1/index.php/sales/add" method="post" id="add_item_form">
<label id="item_label" for="item">
</form>
I have tried jquery but still does not work. Any help would be greatly appreciated.
You could use iframes or xml http requests (ajax)
You absolutely need jquery ajax function to do that. Maybe you have tried different jquery function. Check this out:
http://api.jquery.com/jQuery.post/
There are some examples in that link you can look at.
Do you know the path of your controller? Then you can use jQuery to do that. Check this plugin: http://jquery.malsup.com/form/

Categories