Hi guys, I am new here in the community. I need help on how to pass a javascript variable value to a php variable.. I am using jquery to hide form1 when the page loads. My first javascript is working fine. my code is like this:

<script type="text/javascript">
    	$(document).ready(function(){
		$('#email').hide();
	});
</script>

and here is my php code below:

<?php 
     $url = '';
?>
<form id="form1" method="post" action="<?=$url?>">
	<label>E-Mail: 
    	<input type="text" name="email" />
        <input type="submit" name="go" value="Go" />
	</label>
</form>
<form id="form2" method="post">
	<label>Test: <input type="submit" name="test" /></label>
</form>

then in this code below, I want to change the value of the $url variable to a specific page, example

$url = 'test1.php';
<script type="text/javascript">
	$("form#form2").submit(function(){
		$("form#form1").show();
		$("#tests").hide();
		<?php $url = 'test1.php'; ?>
		return false;
	});
	$("form#form2").submit(function(){
		$("form#form3").show();
		$("#tests").hide();
		<?php $url = 'test2.php'; ?>
		return false;
	});
</script>

the code above doesn't seem to work. I think I am messing something here..

Thanks

Recommended Answers

All 6 Replies

you cannot use 2 html form tags in same php file.

alter your code to 1 html form.. and for action use :

<form id="form1" method="post" action="">
 //eg:
input type="submit" name="go" value="Go" onClick="form1.action=='<?=$url?>'" />

yeah you really mess something.
take a look at your jquery code.

<script type="text/javascript">
    $("form#form2").submit(function(){
    $("form#form1").show();
    $("#tests").hide();
    <?php $url = 'test1.php'; ?>
    return false;
    });
    $("form#form2").submit(function(){
    $("form#form3").show();
    $("#tests").hide();
    <?php $url = 'test2.php'; ?>
    return false;
    });
    </script>

-->> my question to you, do you really understand this code?
where is the form id= form3?
----------------------------------------------------------------------------------

you cannot use 2 html form tags in same php file.

alter your code to 1 html form.. and for action use :

<form id="form1" method="post" action="">
 //eg:
input type="submit" name="go" value="Go" onClick="form1.action=='<?=$url?>'" />

did you mean I should put the other form in another php file and just include it in the main file?

It still didn't work for me guys...

did you mean I should put the other form in another php file and just include it in the main file?

inputs should be in the same form but different actions.

this sample submit form to two different pages.

<form id="form1" method="post" action="">
<input type="submit" name="go" value="Go" onClick="form1.action=='eg1.php'" />
<input type="submit" name="test" onClick="form1.action=='eg2.php'" />
</form>

inputs should be in the same form but different actions.

this sample submit form to two different pages.

<form id="form1" method="post" action="">
<input type="submit" name="go" value="Go" onClick="form1.action=='eg1.php'" />
<input type="submit" name="test" onClick="form1.action=='eg2.php'" />
</form>

Hi Ichcha, i tried your code but it did not work that way I want it.

For example, a user clicks on the button from form2 then the $url value should also change. There will be form2 and form3 width different pages assigned.

example scenario is this. A user clicks on the button of form2 then it marks the users choice by assigning a value of page1.php to the variable $url, then form2 will be hidden and form1 will be shown, then when the button of the form1 is clicked, the user will be redirected to page1.php.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.