setting session or php variable in javascript? - php

i am doing a simple html form for two languages(English & Hindi) so i show the link for both language for ex English / <a href="#">Hindi after that i need to display the form with corresponding language what user clicked one of the hyperlink. so here how do i find out which language link is clicked?
English / <a href="#">Hindi
<? if($_SESSION['language']=='English'){ ?>
<form action="" method="post" name="englishform">
......
</form>
<? } else { ?>
<form action="" method="post" name="hindiform">
......
</form>
<? } ?>

You can not change PHP code with java script. As PHP code is run on server. Sent to client and then a java script can be run on the result. So you have two options:
Either send both forms. Use css to hide both. An onclick show the corresponding form.
Or the AJAX soltion. Then the user clicks on the link. Use java script to fetch the form from the server (an other URL) and show in in the page.

You could add a hidden field to both forms, containing the selected language.
<form action="" method="post" name="englishform">
<input type="hidden" name="language" value="en" />
...
</form>
<form action="" method="post" name="hindiform">
<input type="hidden" name="language" value="hi" />
...
</form>

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.js"></script>
<script type="text/javascript">
$(function() {
$("#showEnglishForm").click(function()
{
$("#hindiFormDiv").hide();
$("#englishFormDiv").show();
});
$("#showHindiForm").click(function()
{
$("#hindiFormDiv").show();
$("#englishFormDiv").hide();
});
});
</script>
</head>
<body>
show English form
show Hindi form
<div id="englishFormDiv" style="display:none;">
<h3>English form</h3>
<form action="" method="post" name="englishForm">
...
</form>
</div>
<div id="hindiFormDiv" style="display:none;">
<h3>Hindi form</h3>
<form action="" method="post" name="hindiForm">
...
</form>
</div>
</body>
</html>

Well, assuming you just want to hide or show a different form based on the hyperlink:
<a class="switcher" rel="englishform" href="#"> English </a> / <a class="switcher" rel="hindiform" href="#">Hindi</a>
<form action="" method="post" id="englishform" style="display:none">
......
</form>
<form action="" method="post" id="hindiform" style="display:none">
......
</form>
<script>
$("a.switcher").click(function(e){
var name = $(this).attr("rel");
$("#" + name).show();
e.preventDefault();
});
</script>

I don't know javascript well enough, but if you get jQuery you can do the following:
English / Hindi
<form action="" method="post" data-lang="en">
...
</form>
<form action="" method="post" data-lang="hi">
...
</form>
jQuery: (needs to be specified more on your actual page or it will target all links and forms)
$('a').click(function(e){
e.preventDefault();
$('form').hide();
$('form[data-lang="'+$(this).attr('href')+'"]').show();
});
Or without jQuery:
<? if(!$_GET['lang'] || ($_GET['lang'] != "en" && $_GET['lang'] != "hi")){ ?>
English / Hindi
<? }elseif($_GET['lang'] == "en"){ ?>
<form action="" method="post">
...
</form>
<? }elseif($_GET['lang'] == "hi"){ ?>
<form action="" method="post" data-lang="hi">
...
</form>
<?
}
?>

Related

Getting post data from form with iframe doesn't work PHP

I have this code:
<!DOCTYPE html>
<html>
<body>
<iframe name="votar" style="display:none;"></iframe>
<form id="excel form" method="post" target="votar">
<input type="submit" name="test" id="test" value="RUN" /><br/>
</form>
</body>
</html>
<?php
if(isset($_POST['test']))
{
echo "hello world";
}
?>
what am I trying to do? well I try to get hte post data from this form without reloading the page and without using ajax, but what am I doing wrong? I tried looking around, but all the other solutions are to long or just not prectical for my website. please help.
EDIT
just changed submit to test, doesn't matter.
<form action="" method="post" >
<!-- code -->
</form>

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';
}
}
?>

Only getting one element from html array in my php file

I have a select in my HTML form:
<form name="correo" id="correo" method="post" action="#" enctype="multipart/form-data" onSubmit="<!--return checkFields();-->" ><div class="multi-field-wrapper" name="multi-field-wrapper">
<div class="multi-fields" name="multi-fields">
<div class="multi-field" name="multi-field">
<div>
<label for="penviadas[]"> Cantidad </label>
<input type="number" name="penviadas[]" class="penviadas" id="penviadas" maxlength="70" placeholder="¿Cuántas?" onClick="removerIcon('iconcant');" >
</div>
</div>
</div>
<button type="button" class="add-field">Añadir otra referencia</button>
</div>
And I can add fields dynamically (or what's the same, I can repeat the code above many times; the code below works).
$('.multi-field-wrapper').each(function() {
var $wrapper = $('.multi-fields', this);
$(".add-field", $(this)).click(function(e) {
$('.multi-field:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
});
$('.multi-field .remove-field', $wrapper).click(function() {
if ($('.multi-field', $wrapper).length > 1)
$(this).parent('.multi-field').remove();
});
});
Let's say I have 3 different selects (penviadas). What I want is to get all of them in my PHP file once I submit the form. It used to work, but now, for some reason, I can only get THE FIRST select (penviadas). Why am I not getting all the values from penviadas array?
PD: I print it in my PHP in different ways but they all return ONLY THE FIRST ELEMENT from penviadas, not the rest:
var_dump($_REQUEST['penviadas']);
I figured it out after many hours playing stupid.
With the information in the OP it was impossible to discover where there was a problem. I found the solution here: Submitting form from different <div> HTML
Basically, I had this structure:
<div...
<form...
</div...
</form>
I thought it was alright and didn't think for a moment this could be affecting. Thus, it wasn't the PHP/JS but the html tags that were incorrect. Thanks for your time.
PHP Code :
<?php
if(isset($_POST['test']))
{
$data = $_POST['referenciasnuevas'];
foreach ($data as $key => $value) {
echo $value . "<br />";
}
}
?>
Html Code:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<form method="post" action="#">
<div class="multi-field-wrapper" name="multi-field-wrapper">
<div class="multi-fields" name="multi-fields">
<div class="multi-field" name="multi-field">
<label for="referenciasnuevas[]">Referencia pieza</label>
<select name="referenciasnuevas[]" id="referenciasnuevas" class="referenciasnuevas" style="width: 105px" onClick="removerIcon('iconref');">
<option selected value='-1'> ¿cliente? </option></select>
</div>
</div>
<button type="button" class="add-field">Añadir otra referencia</button>
</div>
<input type="submit" name="test" value="Submit">
</form>
<script>
$('.multi-field-wrapper').each(function() {
var $wrapper = $('.multi-fields', this);
$(".add-field", $(this)).click(function(e) {
$('.multi-field:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
});
$('.multi-field .remove-field', $wrapper).click(function() {
if ($('.multi-field', $wrapper).length > 1)
$(this).parent('.multi-field').remove();
});
});
</script>
</body>
</html>
The above code works for me.please look my code

need popup window inside a mysql table

i have a simple website which is written by php and mysql code. i have a detect button on my my sql table query page and given below code is writen for this function but my problem is i need a popup window when the detect link is clicked. i have tired to set a code in my created code but i am not able .kindly please help me solve this problem.
<?php $sezione="home_admin"; if(isset($_POST['messaggio']))
$messaggio=$_POST['messaggio'];
include("control_admin..php");
$canc_id=$_GET['canc_id'];
$idcorsocanc=$_POST['idcorsocanc'];
$action=$_REQUEST['action'];?>
<?php
/*echo "permessi".$permessi;
echo "<br>id".$id_nome;*/
if($action=='canc'){?>
<h1>are you sure want to delect the course?</h1>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="canc1" style="float: left; margin-left:25px;">
<input type="hidden" name="idcorsocanc" value="<?=$canc_id?>">
<input type="hidden" name="action" value="">
<input type="submit" name="ok" value="Si,cancella" class="puls_invia">
</form>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="canc2" style="float: left; margin-left:25px;">
<input type="hidden" name="action" value="">
<input type="submit" name="ok" value="NO" class="puls_invia">
</form>
<?php
}
ok i want to update my question cause i follow one answer and here the code is-
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function() {
$('#ok').click(function(){
if(confirm('Are you sure ?')){
$('#form').submit();
}else{
return false;
}
});
});
</script>
</head>
<body>
<?php
if(isset($_POST['action'])){
if($_POST['action'] == 'deleted'){
//the form has been sent, do something
}
}else{
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" id="form">
<input type="button" id="ok" name="ok" value="Delete">
<input type="hidden" id="action" name="action" value="deleted">
</form>
<? } ?>
</body>
</html>
but till now my problem is i alreday have link name delect and if i click that link i saw another delete button cause now i use the following code which i just update then if i click there i saw the pop up window but if i click ok that course is not delete cause i guess something is missing.
my actual need is i alreday have delect link and i need something that if i click on that i saw one opoup window.just this is my need.
You need a client-side script to manage this. I'd recommend something in jQuery.
<script type="text/javascript">
$(document).ready(function(){
$(".myButton").click(triggerPopup);
})
function triggerPopup(){
//do popup stuff
}
</script>
an example in more details can be found by googling. something like this http://istockphp.com/jquery/creating-popup-div-with-jquery/
You should do this in javascript. Especially with jquery library
This should look like this :
<?php
include("control_admin.php");
$sezione = "home_admin";
$canc_id = $_GET['canc_id']; //i'm gessing this is the ID to delete ?
?>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function() {
$('#ok').click(function(){
if(confirm('Are you sure ?')){
$('#form').submit();
}else{
return false;
}
});
});
</script>
</head>
<body>
<?
if(isset($_POST['action'])){
if($_POST['action'] == 'deleted'){
$id = $_POST['id'];
$sql = "delete from table_name where column_id = ".$id;
mysql_query($sql);
echo $canc_id . ' has been deleted!';
}
}else{
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" id="form">
<input type="button" id="ok" name="ok" value="Delete">
<input type="hidden" id="action" name="action" value="deleted">
<input type="hidden" id="id" name="id" value="<?=$canc_id?>">
</form>
<? } ?>
</body>
</html>

Why image didn't appear and display in another page of php after submitted?

I going to design 2 php page,one is personal detail form must filled by the users and another one is display all personal details where they had been done in personal detail form after submit.My problem is that the image that had submitted didn't display on the 2nd page.What wrong with my code?My code shown as below:
<!DOCTYPE html>
<head>
<title></title>
//for preview a image
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
</script>
</head>
<body>
<form name="rform" method="get" action="researchers.php">
<h4>Researchers Profile</h4>
<fieldset>
<legend>Personal Details</legend>
<form id="form1" method="get" action="researchers.php" enctype="multipart/form-data" id="pro_image">
<input type='file' onchange="readURL(this);" name="image" id="image"/>
<br><img id="blah" src="profile pic.jpg" alt="your image" width="160px" height="120px"/><br/>
</form>
<input type="submit" name="savebtn" value="Save"/>
</form>
The researchers.php page
<?php
$name=$_FILES['image']['name'];
$tmp=$_FILES['image']['tmp_name'];
$new=time().$name;
$new="upload/".$new;
move_uploaded_file($tmp,$new);
if($_FILES['image']['error']==0)
{
?>
<br /><img src="<?php echo $new;?>" width="100" height="100"/>
<?php
}
?>
Try changing your <form> method to post as from the php manual for $_FILES - http://www.php.net/manual/en/reserved.variables.files.php
Description
An associative array of items uploaded to the current script via the HTTP POST method.
You will need to also add enctype="multipart/form-data" - http://www.w3.org/TR/html401/interact/forms.html#adef-enctype
<form name="rform" method="post" action="researchers.php" enctype="multipart/form-data">
see also http://www.php.net/manual/en/features.file-upload.post-method.php

Categories