My code (php/js) confuses when I use a variable followed by a number that is bigger than 9
Hello my problem is with my php/javascript code. My variables start from extra1 and ends at extra 12.
The problem is that extra10 , extra11 and extra12 that have two digits confuses the script like there is nothing more than 9.
My code consists of many forms and with this javascript I create a nicer url. Instead of mydomain.com/?extra1=&extra2=abs&extra3=def&extra4=
it creates this mydomain.com/?extra2=abs&extra3=def
for the php script to get from the url and continue.
Everything works great for variables until extra9.
This is my javascript
<script type="text/javascript">
function formSubmit() {
var extra1 = document.getElementById('extra1');
var extra2 = document.getElementById('extra2');
var extra3 = document.getElementById('extra3');
var extra4 = document.getElementById('extra4');
var extra5 = document.getElementById('extra5');
var extra6 = document.getElementById('e开发者_如何学编程xtra6');
var extra7 = document.getElementById('extra7');
var extra8 = document.getElementById('extra8');
var extra9 = document.getElementById('extra9');
var extra10 = document.getElementById('extra10');
var extra11 = document.getElementById('extra11');
var extra12 = document.getElementById('extra12');
if (extra1.value == '') {
extra1.parentNode.removeChild(extra1);
}
if (extra2.value == '') {
extra2.parentNode.removeChild(extra2);
}
if (extra3.value == '') {
extra3.parentNode.removeChild(extra3);
}
if (extra4.value == '') {
extra4.parentNode.removeChild(extra4);
}
if (extra5.value == '') {
extra5.parentNode.removeChild(extra5);
}
if (extra6.value == '') {
extra6.parentNode.removeChild(extra6);
}
if (extra7.value == '') {
extra7.parentNode.removeChild(extra7);
}
if (extra8.value == '') {
extra8.parentNode.removeChild(extra8);
}
if (extra9.value == '') {
extra9.parentNode.removeChild(extra9);
}
if (extra10.value == '') {
extra10.parentNode.removeChild(extra10);
}
if (extra11.value == '') {
extra11.parentNode.removeChild(extra11);
}
if (extra12.value == '') {
extra12.parentNode.removeChild(extra12);
}
return true;
}
</script>
and this is the code from the submitted php that gets the data from the url. it's used on wordpress but is a general php programming
for ($i = 1; array_key_exists('extra'. $i, $_GET); $i++) {
$args['meta_query'][] = array(
'key' => 'extra'. $i,
'value' => $_GET['extra'. $i],
'compare' => '=',
);
}
$query = new WP_Query( $args );
As I said everything works fine for variables < 10.
Thank you for your info
In your code, any extraXX
which is blank would cause your for loop
on the server-side to exit. This means that any extraXX
field after the blank one, would not get processed. In order to get around this, you should really be using input arrays (name your fields as extra[]
in your html) but if you want to keep all the code you have currently, you can do this:
<?php
foreach ($_GET as $key => $value)
{
if (substr($key, 0, 5) == 'extra')
{
$args['meta_query'][] = array(
'key' => $key,
'value' => $value,
'compare' => '='
);
}
}
This will loop through every $_GET
parameter and check to see if it beings with extra
, and if it does, add it to your array.
It fails because in alphabetic order, extra10 comes right after extra1. When it finds out that there's no value for extra10, it stops checking for other fields.
Although there are nicer solutions for this problem, using always 2 digitas may work (for instance, extra01, extra02, etc).
To solve your PHP problem, you could do this:
foreach ($_GET as $key=>$value)
{
if (preg_match("@extra(\d+)@", $key))
{
$args["meta_query"][] = array
( "key" => $key
, "value" => $value
, "compare" => "="
)
}
}
$query = new WP_Query( $args );
Your previous code didn't work, because the first non-existing key would cause the for loop to break. For instance, if the 9th field was removed with JavaScript, the loop would have exited at the 8th, leaving all the other ones.
The script above scans all $_GET variables, and only treats those corresponding to the given RegExp pattern (extra followed by any number of digits).
Also, your JavaScript could be a lot shorter and easier to understand like this:
function formSubmit() {
for (var i=1 ; i<13 ; i++)
{
var extra = document.getElementById('extra' + i);
if (extra.value == '')
{
extra.parentNode.removeChild(extra);
}
}
return true;
}
I don't think this is a good idea, it does nothing of any value unless you are expecting your users to read the URL. Anyway, you can just cycle through the form controls and disable any whose value is '', e.g.:
<script type="text/javascript">
function disableEmpties(form) {
var el, elements = form.elements;
var i = elements.length;
var dontDisableTypes = {
submit: 'submit',
reset: 'reset'
};
while (i--) {
el = elements[i];
alert(el.name + ': ' + el.value);
if ((!el.value || el.value == '') && !(el.type in dontDisableTypes)) {
el.disabled = true;
}
}
}
</script>
<form action="#" onsubmit="disableEmpties(this)">
<input name="foo">
<input name="bar">
<input name="glum">
<input type="submit">
</form>
精彩评论