Page 1 of 1

replacing a form depending if link clicks

Posted: Sat Dec 09, 2006 12:44 pm
by mr-monkey
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello all!!!! new to the forum and php nightmare!

iam having trouble with this script iam trying to make what iam trying to do it have a 2 forms in 1 document but only display 1 at a time depending on the condition 

ive got a form which is used to add to a mysql table and 1 to update i want the add 1 to be visable all the time until the user clicks edit then it gets replaced by a update form

this is the code for the update query would i need to out it in some sort of if statment like if $_Get['editreult'] is clicked echo Update form else echo Add form??? new to php so help with be greatful

Code: Select all

<?php
include 'mysql_connect.php';
//Update Match Result
$editid= $_GET['editresult'];
$query="SELECT * FROM war_results WHERE id='$editid'";
$results=mysql_query($query);
$num=mysql_numrows($results);
mysql_close();
$i=0;
while ($i <$num){
	$team=mysql_result($results,$i,"team");
	$ladder=mysql_result($results,$i,"ladder");
	$opp_name=mysql_result($results,$i,"opp_name");
	$pub_score=mysql_result($results,$i,"pub_score");
	$opp_score=mysql_result($results,$i,"opp_score");
	$website=mysql_result($results,$i,"website");
	
	++$i;
echo '<form  action="upload.php" method="post">';
echo '<input type="hidden" name="id" value="'.$editid.'>">';
echo "<label><script>DateInput('orderdate', true, 'YYYY-MM-DD')</script>";
echo 'Pub Team <select name="team">';
echo '<option value="'.$team.'">'.$team.'</option>';
echo  	'<option value="pub^AS">AfterShock</option>';
echo    '<option value="pub^SZ">ShotZ</option>';
echo  '<option value="pub^Sl.">Slammers</option>';
echo '</select>Ladder';
echo '<select name="ladder" id="ladder">';
echo '<option value="'.$ladder.'">'.$ladder.'</option>';
echo '<option value="edo">ED Open</option>';
echo    '<option value="EDE">ED Entry</option>';
echo    '<option value="EDF">ED Friendly</option>';
echo    '<option value="cb">Clan Base</option>';
echo    '<option value="CBF">Clan Base Friendly</option>';
echo  '</select></label><label>Opponents Tag';
echo  '<input name="opp_name" type="text" id="opp_name" value="'.$opp_name.'" size="10" />';
echo  '</label><label>Opponents Website';
echo  '<input name="website" type="text" id="website" value="'.$website.'" size="20" />';
echo  '<label>PUB Score <input name="pub_score" type="text" id="pub_score" value="'.$pub_score.'" size="3" />';
echo  '</label><label>Opponents Score<input name="opp_score" type="text" id="opp_score" value="'.$opp_score.'"size="3" /></label><input type="submit" name="Submit" value="Submit" /></form>';
}
?>
cheers

Jon


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Dec 09, 2006 6:01 pm
by ok
First of all, you need to check that you got some input:

Code: Select all

if(isset($_GET['editresult']) and $_GET['editresult'] !=null)
{
   $editid= $_GET['editresult'];
}
Secondly, you can replace the while with:

Code: Select all

while($row = mysql_fetch_assoc($results))
Now you can use $row instead of mysql_result:
$row["team"], $row["ladder"]...

See more details in the PHP manual for mysql_fetch_assoc().

P.S
What it does if there no records in the DB?

Posted: Sat Dec 09, 2006 8:37 pm
by califdon
You need to be aware that PHP is server side scripting, that means once it leaves the server there is NO MORE PHP in the document, so it can't possibly react to something the user does after her browser receives the HTML!

Saying it another way, if you want something to happen based on user interaction, you HAVE TO use client side scripting, such as Javascript. The PHP can send the Javascript to the browser, but any action you want to happen must be done in the Javascript.

Posted: Sat Dec 09, 2006 10:45 pm
by mr-monkey
Hi thanks for your help both ive been messing around with what you said ok and got the form to replace when the links clicked. The only problem I have now is getting the update results to appear in the form i can echo the results no problem but when i try to echo them in the form they dont appear i had it working fine with the mysql_results way but the mysql_fetch_assoc seems far more pratical. any ideas why the value arent appearing in the form? heres my modified code

the working code is online at http://www.cheeky-chimp.com/pub/pubwar/ ... esults.php not much to look at, at the minute just trying to sort the code before i make it look pretty

Code: Select all

<?php
include 'mysql_connect.php';


//Update Match Result
$editid= $_GET['editresult'];
$sql="SELECT * FROM war_results WHERE id='$editid'";
$results=mysql_query($sql);

if(isset($_GET['editresult']) and $_GET['editresult'] !=null)
	{

	while($row = mysql_fetch_assoc($results))

			echo '<p>'.$row['date'].'<p></p>'.$row['team'].'<p></p>'.$row['ladder'].'<p></p>'.$row['pub_score'].'<p></p>'.$row['opp_score'].'</p>';//Test to make sure my  query works
			echo '<form  action="upload.php" method="post">';
			echo '<input type="hidden" name="id" value="'.$editid.'>">';
			echo "<label><script>DateInput('orderdate', true, 'YYYY-MM-DD')</script>";
			echo 'Pub Team <select name="team">';
			echo '<option value="'.$row['team'].'">'.$row['team'].'</option>';
			echo '<option value="pub^AS">AfterShock</option>';
			echo '<option value="pub^SZ">ShotZ</option>';
			echo '<option value="pub^Sl.">Slammers</option>';
			echo '</select>Ladder';
			echo '<select name="ladder" id="ladder">';
			echo '<option value="'.$row['ladder'].'">'.$row['ladder'].'</option>';
			echo '<option value="edo">ED Open</option>';
			echo '<option value="EDE">ED Entry</option>';
			echo '<option value="EDF">ED Friendly</option>';
			echo '<option value="cb">Clan Base</option>';
			echo '<option value="CBF">Clan Base Friendly</option>';
			echo '</select></label><label>Opponents Tag';
			echo '<input name="opp_name" type="text" id="opp_name" value="'.$row['opp_name'].'" size="10" />';
			echo '</label><label>Opponents Website';
			echo '<input name="website" type="text" id="website" value="'.$row['website'].'" size="20" />';
			echo '<label>PUB Score <input name="pub_score" type="text" id="pub_score" value="'.$row['pub_score'].'" size="3" />';
			echo '</label><label>Opponents Score<input name="opp_score" type="text" id="opp_score" value="'.$row['opp_score'].'"size="3" /></label><input type="submit" name="Submit" value="Submit" /></form>';
	}

else 
	
	{
			echo '<form  action="'.$_SERVER['PHP_SELF'].'" method="post">';
			echo "<label><script>DateInput('orderdate', true, 'YYYY-MM-DD')</script>";
			echo 'Pub Team <select name="team">';
			echo '<option value="pub^AS">AfterShock</option>';
			echo '<option value="pub^SZ">ShotZ</option>';
			echo '<option value="pub^Sl.">Slammers</option>';
			echo '</select> Ladder <select name="ladder" id="ladder">';
			echo '<option value="edo">ED Open</option>';
 			echo '<option value="EDE">ED Entry</option>';
 			echo '<option value="EDF">ED Friendly</option>';
 			echo '<option value="cb">Clan Base</option>';
 			echo '<option value="CBF">Clan Base Friendly</option>';
 			echo '</select></label><label>Opponents Tag';
 			echo '<input name="opp_name" type="text" id="opp_name" size="10" />';
 			echo '</label><label>Opponents Website';
  			echo '<input name="website" type="text" id="website" size="20" />';
  			echo '</label><label>PUB Score';
  			echo '<input name="pub_score" type="text" id="pub_score" size="3" />';
  			echo '</label><label>Opponents Score';
  			echo '<input name="opp_score" type="text" id="opp_score" size="3" />';
  			echo '</label><input type="submit" name="Submit" value="Submit" />';
			echo '<input name="Reset" type="reset" id="Reset" value="Reset" />';
			echo '</form>';
	}
?>
Thanks again for your help

Jon

Posted: Mon Dec 11, 2006 8:57 am
by mr-monkey
been messing around with it all day and still had no joy at getting the results to echo into the form any ideas anyone?

Posted: Mon Dec 11, 2006 9:19 am
by CoderGoblin
Quick look... $_GET = <form method="GET",
$_POST = <form method="POST"


Looks like you are trying to get $_GET when your form is using POST.

Posted: Mon Dec 11, 2006 10:14 am
by mr-monkey
hmmm abit lost but i need to get the values from the database using the get function so that they can be updated, is the form method="POST" idol until i click the submit button at that time it runs upload which is all post?

not really sure what iam talking about to be honest new to php

Posted: Tue Dec 12, 2006 4:40 am
by neel_basu
Your Problem Can Be Solved Easily By Client Side javaScript Only Without any Hardcoded php.
You Should First Design Your Page With JavaScript
And Then Use

Code: Select all

echo "<script src=\"script.js\"></script>";
Then Use Your HTML Form

You Can Use

Code: Select all

divname.innerHTML
To Do This Job

Posted: Tue Dec 12, 2006 5:24 am
by CoderGoblin
The determination of using $_POST or $_GET in your PHP is based on your HTML form. The interaction with the database does depend on either.
If you need to pass the editresult information with the form you could save it as a hidden field .

Code: Select all

<?php
if (!empty($_GET['editresult'])) {
  $editresult=$_GET['editresult'];
} elseif (!empty($_POST['editresult'])) {
  $editresult=$_POST['editresult'];
}
echo '
<form method="POST>
<input type="hidden" name="editresult" value="'.$editresult.'" />
</form>';
?>
The first time you load the page it uses the $_GET value. When you post the data it uses the $_POST value of the hidden field.

The alternative is to change the form information as, at a guess $_SERVER['PHP_SELF'] does not contain the edit result.

Code: Select all

'<form action="'.$_SERVER['PHP_SELF'].'?editresult='.$_GET['editresult'].'" method="post">';
You can check this by looking at the url of the submitted form (does the address have '?editresult' in. If so you can use $_GET.