...

View Full Version : i'm stuck and don't know how to code this logically...



DiarYofaMadmaN
08-19-2004, 12:05 AM
<script language="text/javascript">
var CatID <? echo'= '. $_GET['CatID']; ?>;
var SectionID <? echo'= '. $_GET['SectionID']; ?>;
var SearchID <? echo'= '. $_GET['SearchID']; ?>;

var arrayurl = new Array(3);
arrayurl[0] = "CatID";
arrayurl[1] = "SectionID";
arrayurl[2] = "SearchID";

var useurlvar = "";
function urlvars() {
for (i=0; i<3; i++) {
if () {

}
//var url = "location.href=CatID=3&SectionID=advanced-search&SearchID=" + document.form1.select.options[document.form1.select.selectedIndex].value + ".php";
}
</script>
The array has the list of variables that are in the url and i'm trying to use a for loop to loop through the arrays which have the variable names and see if the variable names are blank. In the future i will be adding more variables to the array to check. After it finds which ones are not empty add them to the useurlvar and the useurlvar will be used in the location.href....

My logic is probably way off because i was going to use if (arrayurl[i] != "") but that will check to see if the array is blank which technically isn't because it has the variable name in it... I think I'm on the right track but can anyone help me out?

-Thanks

glenngv
08-19-2004, 03:38 AM
Why not simply put the values to the elements of the array directly?

var arrayurl = new Array(3);
arrayurl[0] = "<? echo $_GET['CatID']; ?>"
arrayurl[1] = "<? echo $_GET['SectionID']; ?>";
arrayurl[2] = "<? echo $_GET['SearchID']; ?>";

jamescover
08-19-2004, 03:57 AM
The array has the list of variables that are in the url and i'm trying to use a for loop to loop through the arrays which have the variable names and see if the variable names are blank. In the future i will be adding more variables to the array to check. After it finds which ones are not empty add them to the useurlvar and the useurlvar will be used in the location.href....

Your example is confusing, because some of your variables are predefined:


//var url = "location.href=CatID=3&SectionID=advanced-search&SearchID=" + document.form1.select.options[document.form1.select.selectedIndex].value + ".php";

The script will depend on whether your form fields are required. If so, it would be easier to just check to see that they are filled in, alert if they are not, and don't submit the form until they are.

If some of your variables really are predefined, just create hidden form fields containing the predefined values, and pass them in the url string:


<form name="oFrm" method="get" action="http://www.myDomain.com/myScript.php">
<input name="CatID" type="hidden" value="3" />
<input name="SectionID" type="hidden" value="advanced-search" />
<select name="SearchID">
<option value="val1">Search Term 1</option>
<option value="val2">Search Term 2</option>
<option value="val3">Search Term 3</option>
<option value="val4">Search Term 4</option>
</select>
<input name="oBtn" type="button" value="submit" onclick="javascript:this.form.submit();" />
</form>


var CatID <? echo'= '. $_GET['CatID']; ?>;
var SectionID <? echo'= '. $_GET['SectionID']; ?>;
var SearchID <? echo'= '. $_GET['SearchID']; ?>;

These above can go in your php script:

$CatID = $_GET[CatID];
$SectionID = $_GET[SectionID];
etc.

But, again, if they are predefined:

$CatID = 3;
$SectionID = "advanced-search";
etc.



SearchID=" + document.form1.select.options[document.form1.select.selectedIndex].value + ".php"

Your search term/ID is a select element, not text, so I don't know how you want to handle checking it, if at all. You would access it like this:

//js

function chkFrm(){
var doc = document['oFrm']['SearchID'];
alert (doc.options[doc.selectedIndex].value);
}

//end js

Where is the code for your form???


-james

DiarYofaMadmaN
08-19-2004, 05:15 AM
Your example is confusing, because some of your variables are predefined:



The script will depend on whether your form fields are required. If so, it would be easier to just check to see that they are filled in, alert if they are not, and don't submit the form until they are.

If some of your variables really are predefined, just create hidden form fields containing the predefined values, and pass them in the url string:


<form name="oFrm" method="get" action="http://www.myDomain.com/myScript.php">
<input name="CatID" type="hidden" value="3" />
<input name="SectionID" type="hidden" value="advanced-search" />
<select name="SearchID">
<option value="val1">Search Term 1</option>
<option value="val2">Search Term 2</option>
<option value="val3">Search Term 3</option>
<option value="val4">Search Term 4</option>
</select>
<input name="oBtn" type="button" value="submit" onclick="javascript:this.form.submit();" />
</form>



These above can go in your php script:

$CatID = $_GET[CatID];
$SectionID = $_GET[SectionID];
etc.

But, again, if they are predefined:

$CatID = 3;
$SectionID = "advanced-search";
etc.




Your search term/ID is a select element, not text, so I don't know how you want to handle checking it, if at all. You would access it like this:

//js

function chkFrm(){
var doc = document['oFrm']['SearchID'];
alert (doc.options[doc.selectedIndex].value);
}

//end js

Where is the code for your form???


-james

i have // to echo out if that's what you wana call it.... my values will be predefined in the url but not all variables will be there, it depends on the links i press. I'm also not checking my form, i'm simply just checking to see if the varibles in my url excist or not and if they do then insert the variables used back into the url with the same values. The reason i'm doing this is because I'm creating a dynamic driven site and some variables will not always be used.

DiarYofaMadmaN
08-19-2004, 05:34 AM
Why not simply put the values to the elements of the array directly?

var arrayurl = new Array(3);
arrayurl[0] = "<? echo $_GET['CatID']; ?>"
arrayurl[1] = "<? echo $_GET['SectionID']; ?>";
arrayurl[2] = "<? echo $_GET['SearchID']; ?>";

this may seem a little confusing but I was hopeing to some how use the variable name in the array to see if the variable is empty not see if the array is empty... is this possible?

glenngv
08-19-2004, 06:02 AM
You can check each element of the array if it's empty or not, the same way you do in normal variables.

if (arrayurl[0] == "") {
//CatID is empty
}

jamescover
08-19-2004, 08:24 AM
Where is the code for your form???

Your form?


my values will be predefined in the url but not all variables will be there, it depends on the links i press.

It would help to see the links as well. From what you've shown so far, I don't know why simple url encoding won't work, even for hyperlinks:

<a href="http://www.myDomain.com/myScript.php?CatID=3&etc.">


I think a more comprehensive description, along with your code so far--script, form, links, etc. would yield better results.



this may seem a little confusing but I was hopeing to some how use the variable name in the array to see if the variable is empty not see if the array is empty... is this possible?


It think you mean something like this:


<script type="text/javascript">
<!--

function chkFrm(){

var doc = document['oFrm'];

var oVal = []
oVal[0] = doc['oTxt0'];
oVal[1] = doc['oTxt1'];
oVal[2] = doc['oTxt2'];

for (var i=0;i<oVal.length;i++){
alert ("Type: "+ oVal[i].type +"\n\r"+ "Value: "+ oVal[i].value);

if (oVal[i].type == "text" && oVal[i].value == ""){
alert("Please, complete all fields!");
}
}
}

//-->
</script>


<form name="oFrm">
<input name="oTxt0" type="text" size="10" />
<input name="oTxt1" type="text" size="10" />
<input name="oTxt2" type="text" size="10" />
<input name="oBtn" type="button" value="Check Form" onclick="javascript:chkFrm();" />
</form>


Just remove the first alert, and you can add any action you want in place of the second. At least, that should get you started...

Or you can just check the form elements array. Hard to tell what you are doing without seeing all of your code.

-james

DiarYofaMadmaN
08-19-2004, 04:28 PM
<script language="text/javascript">
var CatID <? echo'= '. $_GET['CatID']; ?>;
var SectionID <? echo'= '. $_GET['SectionID']; ?>;
var SearchID <? echo'= '. $_GET['SearchID']; ?>;

var arrayurl = new Array(3);
arrayurl[0] = "CatID";
arrayurl[1] = "SectionID";
arrayurl[2] = "SearchID";

var UseUrlVar = "";
function urlvars() {
for (i=0; i<3; i++) {
if (arrayurl[i] != "") {
UseUrlVar+=arrayurl[i];
}
}
alert(UseUrlVar);
}
</script>

calling my function:
<select name="select" onClick="javascript:Call urlvars;">

my website functions through variables in the url whatever they are in the url i include the code using php for my page navigation... i'm working on the advanced search of my searching system... found here: http://www.maxforcepc.com/index.php?CatID=3&SectionID=advanced-search I have a php error which i'll fix once i get this javascript issue fixed when you click the listbox and pick a category, right below that will change accordingly to the category

I was testing out my code and i'm recieving this error:
Error: missing ; before statement
Source Code:
javascript:Call urlvars;

glenngv
08-20-2004, 10:20 AM
<select name="select" onclick="urlvars();">

dumpfi
08-20-2004, 04:59 PM
<script language="text/javascript">
function search(sel) {
sel.form.action += '&select=' + this.options[this.selectedIndex].value;
sel.form.submit();
}
window.onload = init;
function init() {
var s_button = document.getElementById('search');
s_button.parentNode.removeChild(s_button);
}
</script>
...
<form action='advanced-search.php'>
<input type='hidden' name='CatID' value='<?php echo $_GET['CatID']; ?>'>
<input type='hidden' name='SearchID' value='<?php echo $_GET['SearchID']; ?>'>
<select name="select" onChange="search(this);">
<option value="None">None</option>
<option value="Hardware">Hardware</option>
<option value="Software">Software</option>
<option value="Networking">Networking</option>
</select>
<input type='submit' value='Search' id='search'>
</form>
dumpfi



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum