Hello. I am new to php but not to c++;

I'm trying to use a submit button to run some html divs and some gui.

here is an example of this , very basic.

$hourglass1 = strip_tags($_POST['hourglass1']); 
 

if($hourglass1){

echo" MY DIV CONTAINERS";
}

The problem is when i try to click anything else, it sets $hourglass1 back to false again and the div containers that were up close again. In c++ to avoid this, i would predefine variables. This way the variables would remain set. I tried to look this up for php but I do not know what to call it.

Also when If i do something like this , it will be a problem.

$hourglass1 = strip_tags($_POST['hourglass1']); 

if($hourglass1){

$intvalue++;
}

$intvalue will only equil 1 until i click anything else. How can i make $intvalue stay at 1 until i tell it to change? thanks all

Recommended Answers

All 12 Replies

post whole code, and explain more what you are trying to do

<form action='myaccountbuisness.php' method='POST'>

<INPUT TYPE="image" SRC="hourglass1.jpg" ALT="Submit Form" width="40" height="40">
<input type="hidden" name="hourglass1" value="submit">




<?php
if($hourglass1){



$submitadvancedsearch1 = strip_tags($_POST['advancedsearch1']); 
$submitbasicsearch1 = strip_tags($_POST['basicsearch1']); 
$initialsearch1=true;

if($submitadvancedsearch1){
	
$initialsearch1=false;
	}else if($submitbasicsearch1){
$initialsearch1=true;
}

if($initialsearch1== true   ){





echo"<h1>Search</h1>

<form action= 'myaccountbuisness.php' method = 'POST' >

<table>
<tr>
<td>

Your full name
</td>
<td>

<input type='text' name='fullname' value='<?php echo $fullname?>'   style='border : 1px solid #BDBDBD ;' >
</td>
</tr>

<tr>
<td>

Choose a username
</td>
<td>

<input type='password' name='repeatpassword' style='border : 1px solid #BDBDBD ;' >
</td>
</tr>
</table>
<p>

<input type='submit' name='search1' value= 'search'>

</form>

<form action= 'myaccountbuisness.php' method = 'POST' >



<input type='submit' name='advancedsearch1' value= 'Switch To Advanced Search'>



</form>



";

echo"


<div id='AJXMAPJOSMXED'><!-- AJXFILE:ajxmaps3.css -->
	<div id='AJXMAPJOSMXEDmap'></div>
</div>
</td>


";



}else if($initialsearch1 == false){

echo"34";


echo"<h1>Search</h1>

<form action= 'myaccountbuisness.php' method = 'POST' >

<table>
<tr>
<td>

Your full name
</td>
<td>

<input type='text' name='fullname' value='<?php echo $fullname?>'   style='border : 1px solid #BDBDBD ;' >
</td>
</tr>

<tr>
<td>

Choose a username
</td>
<td>

<input type='text' name='username'value='<?php echo $username?>' style='border : 1px solid #BDBDBD ;'>
</td>
</tr>

<tr>
<td>

Choose a password
</td>
<td>

<input type='password' name='password' style='border : 1px solid #BDBDBD ;'>
</td>
</tr>


<tr>
<td>

Repeat your password
</td>
<td>

<input type='password' name='repeatpassword' style='border : 1px solid #BDBDBD ;' >
</td>
</tr>

<tr>
<td>

Choose a password
</td>
<td>

<input type='password' name='password' style='border : 1px solid #BDBDBD ;'>
</td>
</tr>


<tr>
<td>

Choose a password
</td>
<td>

<input type='password' name='password' style='border : 1px solid #BDBDBD ;'>
</td>
</tr>





</table>
<p>

<input type='submit' name='search1' value= 'search'>

</form>

<form action= 'myaccountbuisness.php' method = 'POST' >


<input type='submit' name=basicsearch1' value= 'Switch To Basic Search'>

</form>



";

}}

Basically i have a search there there is a button to switch to a more advanced search. The text box's have random names for now because i haven't got that far. When i click on the switch to advanced search button, with the input name as switch to advanced search, A form with more input text box's replaced the original search form. If i click it again it goes back to normal. The problem is when i cant keep the advanced search option open because if i click anything else, it will close. The if statements are run by true of false POST values. The thing is it works but only right after i clicked on a button. If i click anywhere else the value goes back to false. I want this whole search to appear when i click on a link to it.

<form action='myaccountbuisness.php' method='POST'>

<INPUT TYPE="image" SRC="hourglass1.jpg" ALT="Submit Form" width="40" height="40">
<input type="hidden" name="hourglass1" value="submit">

when i click on a button that's shown in the above form code. it will make make $hourglass1 become true. So my menu shows up right. If i click on any other buttons , $hourglass1 will become false, this will close my whole search. That's what i cant figure out.
I cant keep anything open. In c++ i would define a variable in a header, and set it in the constructor, so when i change the varable it will stay that way. Like in php if i say

$var;
if($hourglass1){

$var++;}

$var will not stay the same if i click on anything other than the hourglass1 button. In c++ if you incriment something it will maintain its value unless you tell it otherwise . This is what i don't get about php.

But what you are trying to do and what is not happening?

Ok simply I am using a submit button and its post to control things on my web site.
I use any post's like this.

$hourglass1 = strip_tags($_POST['somename']);

after i click on the button it should trip this kind of code.

if($hourglass1){

my whole search menu pops up.
}

i also tried this

$var;
if($hourglass1){

$var++;
}

if($var>0){my menu pops up}

but for some reason if i click any other button with a post on its method, $vars value as one doesn't stay so my menu will vanish!!!

Ive want to do this with many different parts of my web site. The problem is when taking a true or false value from a POST off of a button click, it only maintains its bool value on click and until i click any other button with a POST for the method. So i cant keep my menu up or anything else i want to have displayed using a bool value given from a post in a conditional statement. I cant get any variables to maintain their value when working with a button post to control php code.

Is the page available online to view. or its in your computer, becuase I am not able to run it here.

what myaccountbuisness.php file contains?

Its not online, only on my comp. I can explain it here.

$something = POST['asubmition'];

$var;
if ($something){
$var++;

}

This is my problem in a nutshell. When i click on the button in a form with a name set to 'asubmition' , $var will turn incriment from 0 to 1;

Good its working right. But if i click on any other button with a post as its method. $var will turn back to 0. I want var to just keep its value as one, but it wont stay if i click on anything else with a post value. It doesn't make sense to me. This wouldn't happen in c++ so i know it has to do with comparing stuff to a POST. This is all i need to know. thanks for your time

you are mixing c++ and php, its syntax is same but behavior is different. I hope you are familiar with html form posting.
when ever you submit the form all is processed as it is new thing. Your previous states are not kept.

If you want to use variables across the pages you must use $_SESSION variable provided by php

-> you must use session_start(); in the beginning of all php page going to access session variable

<?php
session_start();//this must be first line in all php pages you are using
.
.
.
.
.
$something = POST['asubmition'];


if ($something){
if(isset($_SESSION['var'])) 
   $_SESSION['var']++;//always use session variable if you want them to store values across multiple page or multiple posts
else
   $_SESSION['var']=1;

}

When you click on a submit button, only the values of that respective form elements will be posted on to the file specified by action="abcd.php" file in the <form> tag.
In your case when you click on any other submit button say for ex: search1 , then only the element values of that particular form will be posted to "myaccountbuisness.php" and that would be "fullname" and "repeatpassword" values.
check this like http://in.php.net/manual/en/language.variables.scope.php

Also if you would like to have some values available on subsequent requests, then you will have to use sessions.
http://in.php.net/manual/en/book.session.php

//$_POST is an array with key->value pairs
//key being the 'name' attribute on the input/textarea in the form and value, the value of it when submitted
$hourglass1 = strip_tags($_POST['hourglass1']);

if(ctype_digit($_POST['intvalue']) || is_int($_POST['intvalue']){//check if its a string with all numeric characters or is an integer
$intvalue = $_POST['intvalue'];
}else{//else make it 0
$intvalue = 0;
}
if($hourglass1){
$intvalue++;
}
<input type='hidden' name='intvalue' value='<?php echo $intvalue;?>'/><!-- pass the current intvalue back to the php script next time its ran -->

UPDATE

oh you can also use cookies to save data for later, its not secure and easily lost so no sensitive/important data should be put in them

$expire=time()+31400;//time() returns the time now in seconds + 36000 seconds (10 hours)
//setcookie needs to be called before any output is printed on the page
setcookie("cookiename", "mycookievalue", $expire, "/");// "/" makes it available across the whole domain
$_COOKIE['cookiename'] = "mycookievalue";//make the cookie available on the current page

the cookie will stay available on the persons pc until they are cleared or the time expires, so could keep for weeks

Member Avatar for diafol

As previously mentioned, you have a few options to overcome the internet's statelessness:

1) session variables - these are assigned for a short space of time
2) client-side cookies - these are stored on the user's machine, so should be available for as long as you set them or until the user deletes them.
3) db - you could setup a counter / datetime /session /user login_id (implementation is up to you) and store the value there.

There are other data storages available, but usually depend upon the browser type.
Had a look at this before - haven't used it though - looks nice:

http://pablotron.org/?cid=1557

Thanks alot, its ok now.

Member Avatar for diafol

solved?

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.