4 Replies - 1976 Views - Last Post: 10 June 2012 - 05:51 AM

#1 smaboud  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 22-April 12

javascript pass a variable to an external php file

Posted 09 June 2012 - 08:19 AM

I have a form that sends a dynamically generated number on click to a javascript function,


javascript function


<script language="javascript">

function orderSelect(f1){

prompt ("",f1);

var mySite= f1;
prompt ("Your order Id is:",mySite)
t = 'gS.php?s='+mySite;
t = encodeURI (t);
f1.action = t;

f1.submit();

	}
</script>


Please be noted that prompted messages is correct, and the supposly the number(order id) has to be passed into the php file to be set as a cookie

here is the php file

<?
$q= $_GET['s'];
setcookie("orderIdCookie",$q);
header("Location:editOrder.php");
?>



but it seems that the javascript does not pass anything to the php file. I guess i am not using the right syntax for passing the parameter.

Please help me

Is This A Good Question/Topic? 0
  • +

Replies To: javascript pass a variable to an external php file

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: javascript pass a variable to an external php file

Posted 09 June 2012 - 09:08 AM

Here are a few notes on that code...
function orderSelect(f1){
	// Here you show a prompt with no text, using the "f1"
	// input parameter as the default value - which based on 
	// the rest of the code, and your description, is either 
	// a form reference or an event object - and then you 
	// completely ignore the return value.
	prompt ("",f1);
 
	// And here you create a new variable and set it to
	// be equal to "f1"...
	var mySite= f1;
	
	// Which you, again, use as the default value for another
	// prompt, where you, again, ignore the return value!
	prompt ("Your order Id is:",mySite)
	
	// And here you create your URL, in which you use the
	// UNCHANGED "f1" parameter (copied into the "mysite"
	// variable) as the value for the "s" GET value...
	t = 'gS.php?s='+mySite;
	
	// This may be a good idea, assuming "mySite" was actually
	// something that made sense. However, you are usually better
	// of using the encodeURIComponent() function on each
	// value inserted into a query string, rather than using
	// encodeURI() on the entire URL.
	t = encodeURI (t);
	
	// OK, here you use the still unchanged "f1" as if it
	// is a <form> element? - Is it? Because if it is, nothing
	// you do above makes sense. If it is not, then this here
	// makes no sense.
	f1.action = t;
	f1.submit();
}


The bottom line is: the prompt() function returns the value entered into the box, which you need to capture in order to use. Also, your use of f1 in the prompt function calls, which is presumably a form element, makes no sense. What were you trying to achieve with that?
Was This Post Helpful? 0
  • +
  • -

#3 smaboud  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 22-April 12

Re: javascript pass a variable to an external php file

Posted 09 June 2012 - 04:59 PM

well

f1 is a number dynamically generated in table.

those prompts are to test if the function is getting the right number.

the rest is the code i found from another forum, someone had the same issue and that was a suggestion.

what I'm tryng to do is to pass f1.value to another php page.

That's all

please help
Was This Post Helpful? 0
  • +
  • -

#4 smaboud  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 22-April 12

Re: javascript pass a variable to an external php file

Posted 09 June 2012 - 05:25 PM

ive eve tried passing by window location

window.location.href = 'gS.php?s=' + f1;



but no luck
Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: javascript pass a variable to an external php file

Posted 10 June 2012 - 05:51 AM

Quote

what I'm tryng to do is to pass f1.value to another php page.

OK, you have three choices.

  • Pass the value to another PHP page by refreshing the current page, sending the value along in the URL. This is simple, but will cause the current page to be redirected to the other page.
    function orderSelect(f1) {
        window.location = "page.php?s=" + encodeURIComponent(f1);
    }
    
    


  • Pass the value to another PHP page by submitting it through a form. This is essentially the same thing as the previous suggestion, but just using a more complicated method. The plus side of this method is that it allows you to use the POST method, as opposed to the GET method. - This is, presumably, what the last two lines in your code were meant to do, except you didn't have a form to submit. (I won't bother showing you an example of this, because you probably won't want to do this.)

  • The third, and arguably the best, method is to use AJAX. Using AJAX you can send a request to the PHP page without leaving the current page. AJAX is pretty simple to learn; you can pretty much master it in a matter of hours, and it's highly useful. (Actually, it's kind of a requirement in web-development these days, so it's definitely worth the time it takes to learn.)

    I suggest you check out these two tutorials to see what AJAX is all about:
    - @ Google Code University
    - @ tizag.com

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1