codeigniter before submit confirm popup box using javascript - javascript

I need when I click the button for submit view popup box for confirmation and if I press ok, it will continue the submit and if cancel nothing will happen and close the box
here is my code,
code in the view form
<?php
foreach($company_data as $row){
?>
<h2>Company Details.</h2>
<?php echo form_open_multipart('site/update');?>
<?php form_hidden('id', $row->code);?>
<label>Code : </label> <?php echo form_input('code',$row->code);?><br/><br/>
<label>Name : </label> <?php echo form_input('name',$row->name);?><br/><br/>
<label>Logo : </label><input type="file" name="userfile"/><br/><br/>
<label>URL : </label> <?php echo form_input('url',$row->url);?><br/><br/>
<label>Description : </label> <textarea name="description" rows="4" cols="50"><?php echo $row->description; ?></textarea><br/><br/>
<input type="submit" name="submit" value="Update" onclick="update(<?php echo $row->name;?>)"/>
</form>
<?php } ?>

You can put confirm() dialog in your update function like this:
function update(args){
if(confirm('Do you want to submit?')){
// put all the js code here.
}else{
return false;
}
}
So whenever you call your update function it will show a confirm box to ask user to submit the form if user clicks ok then it submits else form won't get submitted.

in onclick function call a javascript function and create confirm box using java script and check the return value from the confirm box if it is true then submit the form by using javascript onsubmit function

here is the answer
<form action="<?php echo site_url('site/update'); ?>" name="myForm" onsubmit="return(validate());">
<?php// echo form_open_multipart('site/update');?>
<?php form_hidden('id', $row->code);?>
<label>Code : </label> <?php echo form_input('code',$row->code);?><br/><br/>
<label>Name : </label> <?php echo form_input('name',$row->name);?><br/><br/>
<label>Logo : </label><input type="file" name="userfile"/><br/><br/>
<label>URL : </label> <?php echo form_input('url',$row->url);?><br/><br/>
<label>Description : </label> <textarea name="description" rows="4" cols="50"><?php echo $row->description; ?></textarea><br/><br/>
<input type="submit" value="Update"/>
</form>
<?php } ?>
<script type="text/javascript">
function validate()
{
var r=confirm("Do you want to update this?")
if (r==true)
return true;
else
return false;
}
</script>

First, here's the classic jQuery way to do this.
$(document).ready(function(){
$( 'form#yourFormId' ).submit( function(){
if( confirm('Really submit this form?') ){
return TRUE;
} else {
return FALSE;
}
})
})
However, your code doesn't appear to submit the form directly, so this probably won't work with your code as written.
You haven't provided the code for the update() method specified in your onClick attribute, but you should be able to add confirmation code to the top of it like this:
function update(name){
if( !confirm('Really submit this form?') ){
return FALSE;
}
// ....the rest of your code...
}

you can use below one line
<?php
foreach($company_data as $row){
?>
<h2>Company Details.</h2>
<?php echo form_open_multipart('site/update');?>
<?php form_hidden('id', $row->code);?>
<label>Code : </label> <?php echo form_input('code',$row->code);?><br/><br/>
<label>Name : </label> <?php echo form_input('name',$row->name);?><br/><br/>
<label>Logo : </label><input type="file" name="userfile"/><br/><br/>
<label>URL : </label> <?php echo form_input('url',$row->url);?><br/><br/>
<label>Description : </label> <textarea name="description" rows="4" cols="50"><?php echo $row->description; ?></textarea><br/><br/>
<input type="submit" name="submit" value="Update" onclick="return confirm(<?= $row->name ?>);"/>
</form>
<?php } ?>

Related

Access session value

How to make a form style hidden if a specific session is set?
I did it that way
<form action="" method="post" <?php if ((isset($_SESSION['start']) )|| (isset($_SESSION['visitor']) )){ echo 'style="display:none;"'; } ?>>
<input type="radio" name="log" id="viso" value="vis" checked onclick="javascript:yesnoCheck();"> Visitor<br>
<input type="radio" name="log" id="uso" value="use" onclick="javascript:yesnoCheck();" > User<br>
<label name="frm" id="frm" style="display:none" ><b>Password</b></label>
<input type="password" style="display:none" name="pw" id="pw" placeholder="Enter Password" >
<input type="submit" >
</form>
<?php
if ( isset ($_POST['log'])&& $_POST['log']=="vis" )
{
$_SESSION["visitor"]="true";
unset($_SESSION["start"]);
}
else if ( isset ($_POST['log'])&& $_POST['log']=="use" )
{
if ( isset($_POST['pw']) && $_POST['pw']!="1111" )
echo "Wrong password";
else if ( isset ($_POST['pw'])&& $_POST['pw']=="1111" )
{
$_SESSION['start']="Welcome";
unset($_SESSION["visitor"]);
}
}
?>
but it's only hidden if i press on the submit button twice not from the first time
You have a simple logical flaw .You are checking for session before you set them .When user fills form and submits , your php runs this
<form action="" method="post" id="lol" <?php if ((isset($_SESSION['start']) )|| (isset($_SESSION['visitor']) )){ echo 'style="display:none;"'; } ?>>
Note that at this time no session is set so form will not have display:noneand will get displayed **even though user has entered his details correctly still when response come form will be visible.Now to fix the issue move your session check before form display code
<?php
if ( isset ($_POST['log'])&& $_POST['log']=="vis" )
{
$_SESSION["visitor"]="true";
//....more stuff
//...
} ?>
<form action="" method="post" id="lol" <?php if ((isset($_SESSION['start']) )|| (isset($_SESSION['visitor']) )){ echo 'style="display:none;"'; } ?>>

How to make a PHP 'if' code work when using auto submit with Java Script?

I have a file upload form that I want to be submited automatically when the file has been selected. I am using Javascript onchange event to do it.
<form action="form.php" method="post" enctype="multipart/form-data">
<input type="file" name="filename" onchange="javascript:this.form.submit();">
</form>
This code seems to work as it is, but I have a PHP if statement that processes the file uploaded when a $_POST['submit'] happens (using the the submit button)
<?php
if(isset($_POST['submit'])){
$photo = Photograph::find_by_userid($user_id);
$photo = new Photograph();
$photo->attach_file($_FILES['photo_upload']);
}
?>
But since the point of this is to remove the submit button <input type="submit" name="submit"/> the PHP if code is not being triggered, ergo the file is not being uploaded. Do you know how can I make the if statement work when not using a submit button and using an auto submit with Javascript?
Thanks in advance!
This is the whole code..
<?php require_once("../includes/initialize.php"); ?>
<?php include_layout_template("header.php"); ?>
<?php include_layout_template("edit_header.php"); ?>
<?php
if(!$session->is_logged_in()){
redirect_to("login.php");
}else{
}
$id=$session->user_id;
$user=User::find_by_id($id);
$user_id = $user->id;
if(isset($_POST['submit'])){
$photo = Photograph::find_by_userid($user_id);
if($photo){
$photo->destroy();
$photo = new Photograph();
$photo->user_id = $user_id;
$photo->attach_file($_FILES['photo_upload']);
if($photo->save()){
redirect_to("edit_photo.php");
$session->message = "Photo uploaded succesfully";
} else{
$message = join("<br/>", $photo->errors);
}
}
$photo = new Photograph();
$photo->user_id = $user_id;
$photo->attach_file($_FILES['photo_upload']);
if($photo->save()){
redirect_to("edit_photo.php");
$session->message = "Photo uploaded succesfully";
} else{
$message = join("<br/>", $photo->errors);
}
}
$photo = Photograph::find_by_userid($user_id);
?>
<section class="edit_section">
<h2>Upload/Change <br/> Your Photo</h2>
<p><?php echo form_errors($errors); ?></p>
<p class="messages"><?php echo $message ?></p>
<form class="edit_form" id="form" action="edit_photo.php" enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
<ul>
<li>
<img src="<?php if(isset($photo)){echo $photo->image_path();} ?>" width="250px" />
<label for="photo_upload">Upload/Change <br/> your photo</label>
<input type="file" id="photo_upload" name="photo_upload" style="background:none; border:none;" value="upload" onchange="javascript:this.form.submit();"/>
<!--input type="submit" name="submit" value="Upload" /-->
</li>
</ul>
</form>
<section style="margin-left: 400px;" >
<p class="button">Delete it</p>
</section>
</section>
<?php include_layout_template("footer.php"); ?>
One solution is to add a hidden field to the form, this field will be recognized by PHP, for example, let's call it "my_flag" :
<form action="form.php" method="post" enctype="multipart/form-data">
<input type="file" name="filename" onchange="javascript:this.form.submit();">
<input type="text" name="my_flag" hidden />
</form>
Now, on the PHP side, you do :
<?php
if ( isset( $_POST['my_flag'] ) )
echo "flag";
?>
The echo "flag"; is just for testing, you replace it by your code.

js submit not works not sending post form data to php

After certain time (2min) when i try form submit using javascript it just submit form without post data value !!! searched solution on web but unable to find solution!! Hope someone can help me
Code:
<script type="text/javascript">
$(function() {
$('.countdown.callback').countdown({
date: +(new Date) + 120000,
render: function(data) {
$(this.el).text( this.leadingZeros(data.min, 2) + "min " + this.leadingZeros(data.sec, 2) + " sec");
if(this.leadingZeros(data.min, 2) < 01 && this.leadingZeros(data.sec, 2) <= 10){
$(this.el).addClass('ended');
}
},
onEnd: function() {
document.getElementById('myform').submit();
}
});
});
</script>
html code with php
<?php
foreach($row as $r){
if(!empty($r['question'])){
?>
<form method="post" id="myform" action="subanswer.php" >
<p><span style="color:rgb(33,79,157);"><?php echo $r['id'];?> <?php echo $r['question'];?></span><br />
<input type="radio" name="<?php echo $r['test_id'].'_'.$r['id'];?>" value="<?php echo $r['ans1'];?>"> <?php echo $r['ans1'];?>
<br>
<input type="radio" name="<?php echo $r['test_id'].'_'.$r['id'];?>" value="<?php echo $r['ans2'];?>"> <?php echo $r['ans2'];?>
<br>
<input type="radio" name="<?php echo $r['test_id'].'_'.$r['id'];?>" value="<?php echo $r['ans3'];?>"> <?php echo $r['ans3'];?>
<input type="hidden" name="testname" value="<?php echo $_GET['id']?>">
</p>
<?php
}
}
?>
<input type="submit" name="subans" value="add" class="but" style="width:15%;" />
</form>
From your code, it looks like you're calling the wrong form:
document.getElementById('myForm').submit();
The ID myFom does not exist. You have:
<form method="post" id="myform1" action="subanswer.php" >
Also not sure why people don't stick to JQuery throughout. Would advise:
$("#myForm1").submit();

SESSION variable slow to update textfield in php

I have a form, contained 3 fields, which when the submit button in submitted,
it will changes the session variable values according to the fields, in this case there are 3 variables. then i echo back the variable onto the fields.
For the first time submit, it stores the value beautifully and display in the fields correctly, the problem is that, when i submit the second time, the values are still the same. after i refresh the page, then the values in the fields are changed.
this is partially the codes i'm using now.
<?php
session_start();?>
?>
<form name="form1" id="form1" action="">
<input type="text" name="acc1" value="<?php echo $_SESSION['acc_main']" />
<input type="text" name="acc2" value="<?php echo $_SESSION['acc_id']" />
<input type="text" name="acc3" value="<?php echo $_SESSION['acc_cat']" />
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']) != '')
{
$_SESSION['acc_main'] = $_POST['acc1'];
$_SESSION['acc_id'] = $_POST['acc2'];
$_SESSION['acc_cat'] = $_POST['acc3'];
}
?>
After i refresh(F5), then the value changed. i want it to be, when i clicked the submit button, it will change to the new value.
PHP Code:
<?php
if(isset($_POST['submit']) != '')
{
$_SESSION['acc_main'] = $_POST['acc1'];
$_SESSION['acc_id'] = $_POST['acc2'];
$_SESSION['acc_cat'] = $_POST['acc3'];
echo '<script type="text/javascript">'
, 'jsfunctionToPrintUpdatedValues();'
, '</script>'
;
}
?>
Javascript Sample Code
function jsfunctionToPrintUpdatedValues()
{
/* retrieve the updated session variables in javascript variables */
var acc_main_js = <?php echo $_SESSION['acc_main']?>
var acc_id_js = <?php echo $_SESSION['acc_id']?>
var acc_cat_js = <?php echo $_SESSION['acc_cat']?>
document.getElementById("main").value=acc_main_js;
document.getElementById("id").value=acc_main_js;
document.getElementById("cat").value=acc_main_js;
}
in the input fields you have to write like this
`
Below
`
<input type="text" name="acc1" value="<?php if(isset($_SESSION['acc_main'])) echo $_SESSION['acc_main']" />
Use if(isset($_POST['submit']) != '') before the form. Change your code to this:
<?php
session_start();
if(isset($_POST['submit']) != '')
{
$_SESSION['acc_main'] = $_POST['acc1'];
$_SESSION['acc_id'] = $_POST['acc2'];
$_SESSION['acc_cat'] = $_POST['acc3'];
?>
<form name="form1" id="form1" action="">
<input type="text" name="acc1" value="<?php echo $_SESSION['acc_main']" />
<input type="text" name="acc2" value="<?php echo $_SESSION['acc_id']" />
<input type="text" name="acc3" value="<?php echo $_SESSION['acc_cat']" />
<input type="submit" name="submit">
</form>
<?php
}else{
?>
<form name="form1" id="form1" action="">
<input type="text" name="acc1" value="<?php echo $_SESSION['acc_main']" />
<input type="text" name="acc2" value="<?php echo $_SESSION['acc_id']" />
<input type="text" name="acc3" value="<?php echo $_SESSION['acc_cat']" />
<input type="submit" name="submit">
<?php } ?>

How to get a msgbox?

I am creating a simple enquiry form with upload option.
If I click submit button it goes to another page and displays success message but it should come in the same page as a message box. What should I do?
Here is my Html code:
<ul>
<li><span>Name</span>
<input name="name" type="text" size="25" class="text1" />
</li>
<li><span>Contact</span>
<input name="contact" type="text" size="25" class="text2" />
</li>
<li><span>E-mail</span>
<input name="email" type="text" size="25" class="text3" />
</li>
<li><span>Subject</span>
<input name="subject" type="text" size="25" class="text4" />
</li>
<li><span>Message</span>
<textarea name="message" size="250" class="text5"></textarea>
</li>
</ul>
<label for="upload"></label>
<div class="x">
<input type="file" name="upload" id="file" class="button">
</div>
<div class="x1">
<input type="submit" name="submit" value="Submit" class="button" />
</div>
And this is my PHP code for uploading and inserting:
<?php
$target = "upload/";
$target = $target . basename($_FILES['upload']['name']);
$ok = 1;
if (move_uploaded_file($_FILES['upload']['tmp_name'], $target))
{
echo "The file " . basename($_FILES['upload']['name']) . " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
?>
<?php
include ("conn.php");
if (isset($_POST['submit']))
{
$sql="insert into cform (name,contact,email,subject,message,upload)values('".$_POST['name']."','".$_POST['contact']."','".$_ POST['email']."','".$_POST['subject']."','".$_POST['message']."','$target')";
$result=mysql_query($sql);
if (!$result)
{
echo "could not enter data";
}
else
{
echo "successfuly entered";
}
}
?>
This PHP page has to run in background and when I click the submit button a message box should display success.
Edit the echo portion of the page as
second page
$rspnse = "";
if(!$result){
$rspnse = "Error";
}
else{
$rspnse = "Success";
}
header('Location: previous_page.php?response='.$rspnse);
Replace the previous_page.php with your original url of the first page
first page
<?php
if(isset($_GET['response'])){
?>
<script>
alert('<?php echo $_GET['response'] ?>');
</script>
<?php
}
?>
Hope this helps...
if(! $result) {
$msg = "could not enter data";
}else{
$msg = "successfuly entered";
}
header('Location: http://www.example.com/form.php?message=$msg');
//Redirect to form page with set message.
add script in form page to display message
if(isset($_GET['message'])){
print $_GET['message'];
}
This solution for as per your code
And another option is using ajax.

Categories

Resources