Add a classname after form is submitted and page reloads - php

Can anyone please help me on this:
I am trying to add a classname/ change the css of div using jquery; after I click submit on the form. Means, after clicking submit and when the page reloads this classname/ changes should stick to the target element. I am using php to get the values of form submitted. (no ajax, but if necessary I can modify my code). I tried to do this by jquery but when the page reloads the changes are gone !
Thanks.
update: Here is the sample code(so.php itself) I am working on. after form is submitted and page is reloaded; say I want to add a class to input that is clicked and manage the style in CSS:
<html>
<head>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input name="Load" type="submit" value="Google" />
<input name="Load" type="submit" value="MSN" />
<input name="Load" type="submit" value="Yahoo" />
</form>
<div id="container">
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
</body>
</html>
update2: I tried the logic provided by #NoPyGod and it worked. But here is the thing. I am trying to add a class to the button that is selected as well as update the text in the div with id "hello" with the value of input that is clicked or in active state once the form is submitted and page is reloaded. This is the one I ended up but once form is submitted
all the inputs are having the same class name as well the text in div is also not updated.
<html>
<head>
<?php if (isset($_GET['Load'])): ?>
<script>
$(document).ready(function() {
$('#hello').text('Hello');
});
</script>
<?php endif; ?>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load" type="submit" value="Google" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load1" type="submit" value="Rediff" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load" type="submit" value="Yahoo" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load1" type="submit" value="Sify" />
</form>
<div id="container" >
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
if (isset($_GET['Load1'])) {
$value = $_GET['Load1'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
<div id="hello"></div>
</body>
</html>

When the page is submitted, set a variable like so
$was_submitted = true;
Then in your HTML do something like this
<div id="myid" class="<?php if ($was_submitted) { echo "some_class"; } ?>">
Or if you absolutely insist on using jQuery to change it, include this in your html --
<?php if ($was_submitted): ?>
<script>
$(document).ready(function() {
$("#myid").addClass("some_class");
});
</script>
<?php endif; ?>
Updated:
<html>
<head>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input name="Load" type="submit" value="Google" />
<input name="Load" type="submit" value="MSN" />
<input name="Load" type="submit" value="Yahoo" />
</form>
<!-- I changed this line below -->
<div id="container" class=<?php if (isset($_GET['Load'])) { echo "some_class"; } ?>>
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
</body>
</html>

Related

Undefined array key when trying to get a value out of a textbox

im currently trying to get a value from a textbox but im always getting undefined array key.
<head>
<!-- Links -->
<link rel="stylesheet" href="./css/vouches.css">
</head>
<body>
<div class="content">
<form method="get"><input type="text" id="oderid" name="oderid" placeholder="oderid / invoiceid"></form>
<form method="post"><input type="submit" name="button1"class="submitbtn" value="Button1" /></form>
<?php
if(array_key_exists('button1', $_POST)) {
button1();
}
function button1() {
$id = $_GET['orderid'];
echo $id;
}
?>
</div>
</body>
</html>
in the part is where the stuff happens. I hope i can get some help, im really new to php
When clicking the button it should echo $id, better said the textbox input
Don't use both POST and GET; pick one.. Like this:
<form method="post">
<input type="text" id="oderid" name="orderid" placeholder="oderid / invoiceid" />
<input type="submit" name="button1" class="submitbtn" value="Button1" />
</form>
<?php
if(array_key_exists('button1', $_POST)) {
button1();
}
function button1() {
$id = $_POST['orderid'];
echo $id;
}
?>
Also take care of your spelling, orderid is not the same as oderid.

HTML Form (with PHP) requires I press submit button twice before it takes me to the action form

I'm new to PHP so please bear with me.
I'm trying to decide the action based on the given input.
Given below is the code I've written for this simple task
<html>
<head>
<meta charset="UTF-8">
<title>SAMPLE</title>
</head>
<body>
<form method="POST" action="<?php
if(isset($_POST['submit_button']))
{
if($_POST['name_field'] === "USERA")
{
echo "output.php";
}
else
{
echo "wronguser.php";
}
}
?>">
Username : <input type="text" name="name_field" value="<?php
if(!isset($_POST['name_field']) || $_POST['name_field']=== "")
{
echo "Enter Something" ;
}
?>">
<input type="submit" name="submit_button" value="SUBMIT BUTTON !">
</form>
</body>
</html>
Here I want the form to redirect to "output.php" if the user is "USERA" or "wrongouput.php" for any other user name entered. Although it works as expected, I'm redirected to the right page only when I press the submit button the 2nd time.
Why is this ?
Also, reloading the page doesn't seem to bring back the original page with the text field containing the default "Enter Something" text. I have to run it all over again from the IDE.
Why is this ?
You can try a different approach
<?php
if(isset($_POST['submit_button']))
{
if($_POST['name_field'] === "USERA")
{
require_once("output.php");
}
else
{
require_once("wronguser.php");
}
}
else
{
?>
<html>
<head>
<meta charset="UTF-8">
<title>SAMPLE</title>
</head>
<body>
<form method="POST" action="">
Username : <input type="text" name="name_field" value="<?php
if(!isset($_POST['name_field']) || $_POST['name_field']=== "")
{
echo "Enter Something" ;
}
?>">
<input type="submit" name="submit_button" value="SUBMIT BUTTON !">
</form>
</body>
</html>
<?php
}
?>
Take your code out of the action, make the action for the form self so it fires on itself, then put your PHP above all the HTML at the top of the file. That'll stop the double click issue.
<?php
if(isset($_POST['submit_button']))
{
if($_POST['name_field'] === "USERA")
{
echo "output.php";
}
else
{
echo "wronguser.php";
}
}
?>">
Username : <input type="text" name="name_field" value="<?php
if(!isset($_POST['name_field']) || $_POST['name_field']=== "")
{
echo "Enter Something" ;
}
?>
<html>
<head>
<meta charset="UTF-8">
<title>SAMPLE</title>
</head>
<body>
<form method='post' action="<?php $SERVER['PHPSELF']; ?>">
<input type="submit" name="submit_button" value="SUBMIT BUTTON !">
</form>
</body>
</html>

Form not sending data via POST

I have this code (it's an include), but the thing is that when I send the form the $_POST data is not being sent.
Im checking for $_POST data like this
if(isset($_POST['ft_upload']) && $_POST['ft_upload'] == 1){
//$usuario -> uploadFirstTime2($db);
echo "ok";
}
and the code for the form is
<div class="ft_userImage" style="background: url(<?php echo $usuario -> getProfileImage(); ?>);"></div>
<p class="ft_step2_continue"><?=$TEXT_BUTTONS['continue'];?></p>
<form action="" method="POST" class="ft_step2_form_upload">
<input type="hidden" name="ft_upload" value="1" />
</form>
<script>
$("p.ft_step2_continue").click(function(){
$(".ft_step2_form_upload").submit();
});
</script>
check.php
<?php
if(isset($_POST['ft_upload']) && $_POST['ft_upload'] == 1) {
echo "ok";
}
?>
index.html
<!DOCTYPE html>
<html>
<head>
<title>form</title>
</head>
<body>
<form action="check.php" method="POST" class="ft_step2_form_upload">
<input type="hidden" name="ft_upload" value="1" />
</form>
<button id="ft_step2_continue">SEND</button>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$("#ft_step2_continue").click(function(){
$(".ft_step2_form_upload").submit();
});
</script>
</body>
</html>
it works fine.
i think, you just forgot action="check.php" in your form tag.
Here check this:
<?php var_dump($_POST); ?>
<html>
<head>
<title></title>
</head>
<body>
<form action="" method="POST" class="ft_step2_form_upload">
<input type="hidden" name="ft_upload" value="1" />
</form>
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script>
$("p.ft_step2_continue").click(function(){
$(".ft_step2_form_upload").submit();
});
</script>
</body>
</html>
Ok so i did this. Saved it in a php file. And then triggered the submit and it outputs:
array(1) { ["ft_upload"]=> string(1) "1" }
Your php code should be in the same file where the form html is written because your action attribute is empty.
You need to specify the action attribute.
<form action="check.php" method="POST" class="ft_step2_form_upload">
I think you want transfer a file to server??? if yes:
<form method="POST" action="this-file-name.php" enctype="multiform/form-data">
Your Photo:
<input type="file" name="filet" required="required">
<input type="submit" value="Send Photo">
</form>
<?php
if(isset($_FILES['filet'])) {
$dir = './my_dir_name/';
$file_name = $_FILES['filet']['name'];
$doit = #move_uploaded_file($_FILES['filet']['tmp_name'],$dir.$file_name);
if($doit) {
echo 'File '.$file_name.' uploaded';
}
}
?>

Deleting objects using checkboxes with Codeigniter

I have read many post like this but have failed to find my particular situation.Trying to delete the selected checkbox. right now you can submit the form and it takes you to all the right pages except it doesn't actually delete anything.
Here is my controller info
function deleteFolder() {
if(array_key_exists('deleteMe',$_POST)) {
$checkbox = $this->input->post['checkbox'];
$this->index_model->deleteFolder($checkbox);
}
$this->folderdeleted();
}
Here is my Model
function deleteFolder($checkbox) {
$this->db->where('folderName', 'folderName');
$this->db->delete('senior', $checkbox);
return;
}
Here is my View
<!DOCTYPE html>
<?php $this->load->view('partials/page_head'); ?>
<body>
<div id="container">
<div id="top">
<div class="topcenter">
<h2><a class="homebtn" href="<?php echo base_url();?>">Home</a></h2>
</div>
<div class="navdescription"><span>Delete Page</span></div>
</div>
<div class="projectFolders">
<?php foreach($foldername as $row) { ?>
<div class="folder">
<button><?php echo $row->folderName; ?></button>
<div class="delete">
<form name="delete" method="post" action="<?php echo base_url(); ?>index.php/home/folderdeleted">
<p>
<input type = "checkbox" id = "check_<?php echo $row->folderName; ?>"/>
<?php echo form_submit('deleteFolder', 'Delete'); ?>
</p>
</form>
</div>
</div>
<?php } ?>
</div>
</div><!-- End of container div -->
</body>
</html>
There are several errors in your code. I'm not sure whether I've found all of them and whether the code will work.
The controller should be:
function deleteFolder() {
if($this->input->post('checkbox') !== false) {
$checkbox = $this->input->post('checkbox');
$this->index_model->deleteFolder($checkbox);
}
$this->folderdeleted();
}
The model should be:
function deleteFolder($checkbox) {
$this->db->where('folderName', $checkbox);
$this->db->delete('senior');
return;
}
The input tag in the view should be:
<input name="checkbox" value="<?php echo $row->folderName; ?>" type = "checkbox" id = "check_<?php echo $row->folderName; ?>"/>
A little warning for checkboxes: if they aren't checked, you won't find anything in the $_POST variable.

why my get_option wordpress function is returning null value when I refresh the page?

I have this code below:
<?php function wp_copickpage()
{ color_option_update(); ?>
<form method="POST" action="">
<?php if (get_option('custom_bg_color') != null ) {?>
<input type="text" id="color" name="color" value="<?php echo get_option('custom_bg_color'); ?>" /> <?php }
else { ?>
<input type="text" id="color" name="color" value="<?php echo get_option('custom_bg_color'); ?>" />
<?php } ?>
<p><input type="submit" name="search" value="Update Options" class="button" /></p>
</form>
<div id="colorpicker"></div>
<?php echo('Color:'); echo get_option('custom_bg_color'); ?>
<link rel="stylesheet" type="text/css" href="<?php echo get_bloginfo('template_url');?>/farbtastic.css">
<script src="<?php echo get_bloginfo('template_url');?>/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="<?php echo get_bloginfo('template_url');?>/farbtastic.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#colorpicker').farbtastic('#color');
});
</script>
<?php }//end of function wp_copickpage
//save the selected color in a wordpress option
function color_option_update()
{ update_option('custom_bg_color', $_POST['color']);}
?>
Everything works fine, except when I refresh the page, the value of get_option('custom_bg_color') is returning null.
However, if I press the update button, it returns the desired value. But if I reload the page, the value of get_option('custom_bg_color') is back to null.
Is there something wrong with my update_option? What did I miss here?
It looks like you are calling
color_option_update()
every time the page is loaded, which means whenever you first load the page it is set as null until you update (since there is no $_POST['color'] variable, because nobody has submitted one on this page load).
Try this:
<?php function wp_copickpage()
{
//heres the changed part
if($_POST['color'] && $_POST['color'] != null) {
color_option_update();
}
?>
<form method="POST" action="">
<?php if (get_option('custom_bg_color') != null ) {?>
<input type="text" id="color" name="color" value="<?php echo get_option('custom_bg_color'); ?>" /> <?php }
else { ?>
<input type="text" id="color" name="color" value="<?php echo get_option('custom_bg_color'); ?>" />
<?php } ?>
<p><input type="submit" name="search" value="Update Options" class="button" /></p>
</form>
<div id="colorpicker"></div>
<?php echo('Color:'); echo get_option('custom_bg_color'); ?>
<link rel="stylesheet" type="text/css" href="<?php echo get_bloginfo('template_url');?>/farbtastic.css">
<script src="<?php echo get_bloginfo('template_url');?>/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="<?php echo get_bloginfo('template_url');?>/farbtastic.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#colorpicker').farbtastic('#color');
});
</script>
<?php }//end of function wp_copickpage
//save the selected color in a wordpress option
function color_option_update()
{ update_option('custom_bg_color', $_POST['color']);}
?>
Edit: Going over my past answers, for future reference the better way to write this would have been to parameterize the color_option_update function rather than use post data directly. So
//save the selected color in a wordpress option
function color_option_update()
{ update_option('custom_bg_color', $_POST['color']);}
Becomes
//save the selected color in a wordpress option
function color_option_update($color)
{ update_option('custom_bg_color', $color);}
And then this
//heres the changed part
if($_POST['color'] && $_POST['color'] != null) {
color_option_update();
}
becomes
//heres the changed part
if($_POST['color'] && $_POST['color'] != null) {
color_option_update($_POST['color']);
}

Categories