Problems accessing element style attribute with javascript - php

I am trying to access the style attribute of a element in order to display a form however, I get this error: Uncaught TypeError: Cannot read property 'style' of undefined. The example below is a simplified example.
cookies.php
echo "[a href='test.php?flavor=butter']Bake Butter Cookies[/a]";
echo "[a href='test.php?flavor=chocolate'] Bake Chocolate Cookies [/a]";
?>
test.php
switch($cookieType){
case "chocolate":
echo "<script type='text/javascript'>";
echo "document.chocolate.style.display='block'; ";
echo "</script>";
break;
case "oatmeal-raisin":
echo "<script type='text/javascript'>";
echo "document.oatmealraisin.style.display='block'; ";
echo "</script>";
break;
case "butter":
echo "<script type='text/javascript'>";
echo "document.butter.style.display='block'; ";
echo "</script>";
break;
}
?>
<div id="chocolate" style="display:none;">
<form action="<?php echo $_SERVER['php_self']?>">
<input type="hidden" name="type" value="chocolate"/>
What is your name: <input type="text" name="your_name"/>
How many chocolate chips on each cookie? <input type="text" name="chips"/>
How many many cookies? <input type="text" name="cookies"/>
<input type="submit" name="BAKE!"/>
<input type="reset"/>
</form>
</div>
<div id="oatmealraisin" style="display:none;">
<input type="hidden" name="type" value="oatmealraisin"/>
<form action="<?php echo $_SERVER['php_self']?>">
What is your name: <input type="text" name="your_name"/>
How many raisins on each cookie? <input type="text" name="raisins"/>
How many many cookies? <input type="text" name="cookies"/>
<input type="submit" name="BAKE!"/>
<input type="reset"/>
</form>
</div>
<div id="butter" style="display:none;">
<form action="<?php echo $_SERVER['php_self']?>">
<input type="hidden" name="type" value="butter"/>
What is your name: <input type="text" name="your_name"/>
How many many cookies? <input type="text" name="cookies"/>
<input type="submit" name="BAKE!"/>
<input type="reset"/>
</form>
I have never had problems accessing the style attribute of a element before so I am not sure what is the problem. The switch statement does work because the javascript console shows the output of the right case. I need another pair of eyes. Any suggestions?

It's not the style attribute that is the problem, you failed to access the elements themselves.
You are trying to access the elements as if they are global variables in the document object, that is only true in IE in quirks mode. You need to use the getElementById method. Replace:
document.chocolate
with:
document.getElementById('chocolate')
and corresponding for each element.

Related

How can I keep the URL id on submit?

When I click submit, the data is submitted to the database but the URL id of the book disappears to ----book.php
I want the URL to go back to the id of the page e.g. ----book.php?id=3
Is it possible to keep the first line as action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" and add the value="<?php echo $book_id ?>"?
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" value="<?php echo $book_id ?>" name="book_id" />
<p>Author: <input type="text" value="<?php echo $_SESSION['author']; ?>" name="author" id="author" readonly /></p>
<p>Summary: <input type="text" name="summary" value="<?php echo $summary;?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
PHP code:
if (isset($_GET['id'])) {
$book_id = $_GET['id'];
}
Please try this code:
// save values in DB
header('Location: book.php?id=' . $book_id);
exit;
It is because $_SERVER["PHP_SELF"] contains only URL . It does not contain get queries . To solve the problem leave your action empty
e.g
<form method="post" action="">.....
You send a POST form but are trying to retrieve a GET value. Additinally, the parameter is named book_id, not id.
Use $book_id = $_POST['book_id'];
if (isset($_GET['id'])) { also won't work, for the same reasons.
There is no name with "id"
If you want to get the book id:
if (isset($_POST['book_id'])) {
$book_id = $_POST['book_id'];
}
OR modify the HTML like this:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" value="<?php echo $book_id ?>" name="id" />
<p>Author: <input type="text" value="<?php echo $_SESSION['author']; ?>" name="author" id="author" readonly />
</p>
<p>Summary: <input type="text" name="summary" value="<?php echo $summary;?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
On the other hand, if you have problems with POST, just modify the first line of HTML for this one:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) . "?id=" . echo $book_id; ?>">
You can use anyone of these:
PHP_SELF returns just URL. Use REQUEST_URI that returns URL with query string:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]);?>">
[you can also omit action values - that will have same behavior]
or if you want just id, then use:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) . "?id=" . $_GET['id'];?>">
Note: Use validation wherever necessary. I just gave you an idea.

how to send checkbox value on next page

how to send check box value in to next page without submit button and without action.
here i am going to next page using link so i want that these checkbox value send on next.php
my form is
<form method="post" action="" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;">
<input name="pay_type" type="checkbox" value="<?php echo $monthly; ?>"id="chk_Box102" onClick="toggle_2(this);" >
Monthly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $yearly; ?>" id="chk_Box103" onClick="toggle_2(this);">
Yearly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $lifetime; ?>" id="chk_Box104" onClick="toggle_2(this);">
Lifetime
</form>
and here a link by using i am going to next page
go here
To post data from one page to another page you have to use a submit button like:
<form method="post" action="next.php" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;">
<input name="pay_type" type="checkbox" value="<?php echo $monthly; ?>"id="chk_Box102" onClick="toggle_2(this);" >
Monthly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $yearly; ?>" id="chk_Box103" onClick="toggle_2(this);">
Yearly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $lifetime; ?>" id="chk_Box104" onClick="toggle_2(this);">
Lifetime
<input type="submit" value="Go here" />
</form>
also specify receiving page in action attribute of <form>
In another page (next.php).
You have to access your posted values using $_POST['name'];
eg.
echo $_POST['pay_type'];
EDIT
In your code, I can see that you want to send multiple values for checkbox pay_type, for that you have to write it as an array (name="pay_type[]")in HTML to send multiple values.
So, the complete code will looks like:
<form method="post" action="next.php" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;">
<input name="pay_type[]" type="checkbox" value="<?php echo $monthly; ?>"id="chk_Box102" onClick="toggle_2(this);" > Monthly<br /><br />
<input name="pay_type[]" type="checkbox" value="<?php echo $yearly; ?>" id="chk_Box103" onClick="toggle_2(this);"> Yearly<br /><br />
<input name="pay_type[]" type="checkbox" value="<?php echo $lifetime; ?>" id="chk_Box104" onClick="toggle_2(this);"> Lifetime
<br/>
<input type="submit" value="Go here" />
</form>
Code in next.php:
<?php
echo "<pre>";
print_r($_POST['pay_type']); //it will print complete array of values for pay_type checkbox
echo "</pre>";
?>
Log1c ツ as said before you need a button.. :). What is also possible to use an attribute onClick:
Add an ID at your form tag (id="myForm") (dont forget the target page next.php in action)
<form method="post" action="next.php" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;" id="myForm">
and then the onClick attribute
go here
jsfilleDemo

Will only display first part of mysql_fetch_array

Although variations of this question have been asked before, none of them have helped me solve this puzzle. at present the form I have put together updates the database fine etc, the issue is when I go to edit the field, only the first part of the data is shown for example, if the data was 'Sunny Day' then when i clicked to edit this field all i would get is 'Sunny', so then if i then clicked update, it would edit the database back to Sunny. Am i missing something?!
Thanks in advance.
// Connects to your Database
$query=mysql_connect("localhost", "cl52-abcdef","abcdef") or die(mysql_error());
mysql_select_db("cl52-abcdef",$query);
?>
<html>
<body>
<?php
if(isset($_GET['id']))
{
$id=$_GET['id'];
if(isset($_POST['submit']))
{
$holdesc1=$_POST['holdesc1'];
$holdest1=$_POST['holdest1'];
$rrp1=$_POST['rrp1'];
$cpe1=$_POST['cpe1'];
$ea1=$_POST['ea1'];
$query3=mysql_query("update DealOne set holdesc1='$holdesc1',holdest1='$holdest1',rrp1='$rrp1',cpe1='$cpe1',ea1='$ea1' where id='$id'");
if($query3)
{
header('location:list.php');
}
}
$query1=mysql_query("select holdesc1, holdest1, rrp1, cpe1, ea1 from DealOne where id='$id'");
$query2=mysql_fetch_array($query1);
?>
<form method="post" action="">
1 <input type="text" value=<? echo $query2['holdesc1']; ?> name="holdesc1"><br>
2 <input type="text" value=<? echo $query2['holdest1']; ?> name="holdest1"><br>
3 <input type="text" value=<? echo $query2['rrp1']; ?> name="rrp1"><br>
4 <input type="text" value=<? echo $query2['cpe1']; ?> name="cpe1"><br>
5 <input type="text" value=<? echo $query2['ea1']; ?> name="ea1"><br><br>
<input type="submit" name="submit" value="update" />
</form>
<?php
}
?>
</body>
</html>
Only the first part of the data is showing up because your value attribute values do not have quotes around it. Otherwise, your HTML looks like: <input type="text" value=Sunny day name="holdesc1">
Change your text inputs to the following:
<input type="text" value="<? echo $query2['holdesc1']; ?>" name="holdesc1"><br>
<input type="text" value="<? echo $query2['holdest1']; ?>" name="holdest1"><br>
<input type="text" value="<? echo $query2['rrp1']; ?>" name="rrp1"><br>
<input type="text" value="<? echo $query2['cpe1']; ?>" name="cpe1"><br>
<input type="text" value="<? echo $query2['ea1']; ?>" name="ea1"><br><br>

Keep form data inside the field after submission using php

I am using below code for a html form.(it has two forms) I am able to keep the textarea field after the first and second form submission. but issue I am facing here is the dropdown menu selection.
Code:
<html>
<body>
<div class="emcsaninfo-symcli-main">
<form id="form1" name="form1" action=" " method="post" >
<div class="input">Your Name</div>
<div class="response"><span><input class="textbox" id="myname" name="myname" type="text" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>" /></span> </div>
<div class="input">Your Place</div>
<div class="response"><span><input class="textbox" id="myplace" name="myplace" type="text" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" /></span> </div>
<div class="input-quest">Graduation Status</div>
<div class="input-resp"><select id="graduation" name="graduation" OnChange="CMT();"><option class="dropdown-options">Graduate</option><option class="dropdown-options">Non Graduate</option></select></div>
<div class="submit">
<input id="first_submit" type="submit" name="first_submit" value="first_submit" />
</div>
</form>
<?php
if(!empty($_POST['myname']) && !empty($_POST['myplace']) || !empty($_POST['output_textarea'] ) )
{
$myname = $_POST['myname'];
$myplace = $_POST['myplace'];
$graduation = $_POST['graduation'];
?>
<form id="form2" name="form2" action=" " method="post" >
<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly value="<?php if(isset($_POST['output_textarea'])) { echo htmlentities ($_POST['output_textarea']); }?>">
<?php
echo "My name is $myname and I am from $myplace, and I am $graduation";
?>
</textarea>
<input id="submit1" type="submit" name="name_field" value="submit1" />
<input id="submit2" type="submit" name="place_field" value="submit2" />
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
</form>
<?php
function name()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['name_field']))
{
name();
}
function place()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['place_field']))
{
place();
}
}
?>
</div>
</html>
</body>
For example if I put name = John, place : UK and selecting graduation status as graduate, it will will give me first form output as in my output textarea
My name is John and I am from UK, and I am Graduate
I have two seperate submit button for second form, using that I am doing some other function with help of the output textarea
If I press any of the second button,I m able to keep entries my name and place area, but it not keeping the dropdown selection. so it will only display like after submitting submit1 or submit2
My name is John and I am from UK, and I am
Here,
How can I keep the the dropdown selection also with the output text area
Will I able to show only the output_textarea content after second form submission without keeping the first form data ?
PHP FIDDLE
You have an error in logic in the hidden input for the "graduate" element.
This is what you have at lines 53-55. Line 55 doesn't have an echo instead it has an $graduation = $_POST['graduation']; which won't help you:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
instead of that, this code should work:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { echo htmlentities($_POST['graduation']); }?>" />

Issues with passing PHP echo in form through POST method

<form method="POST" name="send"
<input type="hidden" name="title" value="<?php echo ($pro->title;?)">
</form>
I dont want people to see the hidden informations , Is this the best way to pass the variable over to my controller?
This the code in my controller
$this->email->subject('subject '.$_POST['title'].' ' );
Thank you!
<form method="POST" name="send"
<input type="hidden" name="title" value="<?php echo ($pro->title;?)">
should be
<form method="POST" name="send">
<input type="hidden" name="title" value="<?php echo $pro->title; ?>" />
For this i will suggest you to use SESSION because hidden fields can be changed.

Categories