Not able to submit multiple forms on single html page - php

I have problem with forms on one file.
The problem is that when I click submit ... it submits the first form data instead of the form that the person filled even though as you can see from the code below that I specified a certain id for each form.
I am using a php file to submit the data to mysql
here is my code:
case 'upd_chpt':
if($_POST['does'] == 'upd_chpt')
{
$chId = $_POST['chId'];
$name = $_POST['name'];
$nums = $_POST['nums'];
$mngs = $_POST['mang'];
$ch_trans = $_POST['ch_trans'];
$ch_down = $_POST['ch_down'];
$ch_translate = $_POST['ch_translate'];
$ch_clean = $_POST['ch_clean'];
$ch_editor = $_POST['ch_editor'];
if(!empty($chId) && !empty($name) && !empty($nums) && !empty($mngs) && !empty($ch_trans))
{
$mnG = explode(',' , $mngs);
$qup = $db->query("UPDATE chapter SET ch_name = '".$name."', chapter_num = '".$nums."', manga_id = '".$mnG[0]."', manga_title = '".$mnG[1]."', manga_name = '".$mnG[2]."', ch_down = '".$ch_down."', ch_translate = '".$ch_translate."', ch_clean = '".$ch_clean."', ch_editor = '".$ch_editor."', ch_trans = '".$ch_trans."' WHERE ch_Id = '".$chId."' ");
if($qup)
{
echo "updated successfully";
}
else
{
echo "erorr ";
}
}
else
{
echo "please fill all the fields !";
}
}
break;
html code :
<form id="edit_{$ch[ch].ch_Id}">
<input type="hidden" name="chid" value="{$mn[mn].ch_Id}" />
<label>chapter number</label>
<input type="text" id="chaptr" name="chaptr" value="{$ch[ch].chapter_num}" class="short" />
<label>chapter title </label>
<input type="text" id="name" name="name" value="{$ch[ch].ch_name}" class="short" />
<label>manga related to chapter</label>
<select id="mansga" name="manga">
{section name='mn' loop=$mng}
<option value='{$mng[mn].mn_Id},{$mng[mn].mn_title},{$mng[mn].mn_name}' {if $mng[mn].mn_Id==$ch[ch].manga_id}selected="true" {/if}>
{$mng[mn].mn_name}
</option>
{/section}
</select>
<label>chapter link</label>
<input type="text" id="ch_down" name="ch_down" value="{$ch[ch].ch_down}" class="short" />
<label>all credit goes to</label>
<input type="text" id="ch_trans" name="ch_trans" value="{$ch[ch].ch_trans}" class="short" />
<label>translated by</label>
<input type="text" id="ch_translate" name="ch_translate" value="{$ch[ch].ch_translate}" class="short" />
<label>cleaned by</label>
<input type="text" id="ch_clean" name="ch_clean" value="{$ch[ch].ch_clean}" class="short" />
<label>edited by</label>
<input type="text" id="ch_editor" name="ch_editor" value="{$ch[ch].ch_editor}" class="short" />
<label><input type="submit" class="submit" value="تعديل" /></label>
</form>
javascript :
$('[id^=edit_]').submit(function(){
var id = $(this).attr('id').split('_')[1];
var name = $('#name').val();
var nums = $('#chaptr').val();
var mang = $('#mansga').val();
var ch_trans = $('#ch_trans').val();
var ch_down = $('#ch_down').val();
var ch_translate = $('#ch_translate').val();
var ch_clean = $('#ch_clean').val();
var ch_editor = $('#ch_editor').val();
$.post("action.php", {does: 'upd_chpt' ,ch_trans:ch_trans, ch_down:ch_down, ch_translate:ch_translate , ch_clean:ch_clean , ch_editor:ch_editor , chId:id , name: name, nums: nums, mang:mang},function(m){
alert(m);
});
return false
});
can this code be changed ? and be only php + html without javascript ! ... because I think that id="" is the problem

the mistake was on the second line
{$mn[mn].ch_Id} should be {$ch[ch].ch_Id}

Related

Not inserting null values

I am trying to insert multiple values in the todo input of the form. but if i do not input a value to any input the null value is inserted in the database. This is my controller action:
public function actionAdd()
{
if (Yii::$app->request->isAjax) {
$request = Yii::$app->request;
$add = new Project();
$add->project_name = $request->post('project');
$add->deadline = $request->post('deadline');
$add->profile_id = $request->post('profile_id');
$add->project_status = "Running";
$add->save();
$getlast = Yii::$app->db->getLastInsertId();
$todo = $request->post('todo');
if (isset($todo)) {
foreach ($todo as $to) {
$add = new Todo();
$add->todo_name=$to;
$add->status="Running";
$add->project_id=$getlast;
$add->save();
}
}
echo json_encode(TRUE); die;
}
echo json_encode(FALSE);die;
}
the form is:
<form class="formclass" method="POST" action="<?php echo Yii::$app->request->baseUrl;?>/todo/add/" role="form" id="register-form" novalidate="novalidate">
<label> Project Name: </label> <input type="name" name="project" class="form-control" placeholder="Project Name" required><br><br>
<input type="hidden" name="profile_id" value="<?php echo $profile_id;?>">
<label> Todo: </label><br>
<textarea type="text" name="todo[]" placeholder="Todo Description..."></textarea>
<div id="dynamicInput">
<span class="glyphicon glyphicon-plus" onClick="addInput('dynamicInput');"></span>
</div><br>
<label> Project Deadline:</label><br>
<input type="date" name="deadline"><br><br>
<button type="submit" class="btn btn-default">Add Project</button><BR><BR>
</form>
And the jquery is:
var counter = 1;
var limit = 10;
function addInput(divName)
{
if (counter != limit)
{
var newdiv = document.createElement('div');
newdiv.innerHTML = "<br><textarea type='text' name='todo[]' placeholder='Todo Description...'></textarea>";
document.getElementById(divName).appendChild(newdiv);
counter++;
}
}
Could be you problem is related to the validation rules ..
for check this and only for debugging try use $add->savel(false)
$add = new Todo();
$add->todo_name=$to;
$add->status="Running";
$add->project_id=$getlast;
$add->save(false);
if in this case the rows are inserted the check for validations rule (eventually comment temporary) .. for find the wrong rules or condition

PHP - Can not access the checkbox value that has been checked

For my databases project I am doing this form where a user can select the information he wants and it will be displayed on a html page. For the time being I am implementing only the checkboxes where I am encountering a problem.
The problem is that when I check the checkboxes (for my current implementation I am only interested in the case where the user checks "Location"+ "Pipe-Type" + "Amount") in my action page "fe_page2.php", the if condition that checks to see if the checkboxes are checked is never being reached instead it always goes to the else condition. The code is provided below.
This is my basic html page:
<html>
<head>
<title>Flow Element</title>
</head>
<body>
<h1 style="margin-left: 450px">Retrieve Flow Element Information</h1>
<hr>
<form action="fe_page2.php" method="post">
All Times:
<input type="checkbox" name="alltimes" onchange="changeFields()"><br><br>
Start Date:
<input type="date" name="startdate" id="startdate">
Start Time:
<input type="time" name="starttime" id="starttime"><br><br>
Same Time:
<input type="checkbox" name="sametime" id = "sametime" onchange="sameFields()"><br><br>
End Date:
<input type="date" name="enddate" id="enddate">
End Time:
<input type="time" name="endtime" id="endtime"><br><br>
<h3> Select fields of output</h3>
<hr>
Pipe Key: <input type="text" name="pipepirmary" id="pipekey"><br>
<input type="checkbox" name="pipelocation" id="pipeLocation" value="value1" >
Location<br>
<input type="checkbox" name="Pipe-Type" id="pipetype" value="value2">
Pipe-Type<br>
<input type="checkbox" name="amount" id="amount" value="value3">
Amount<br><br>
Flow Element:<input type="text" name="fepirmarykey" id="fekey"/><br>
<input type="checkbox" name="flow" id="flo">
Flow<br>
<input type="checkbox" name="temperature" id="temp">
Temperature<br>
<input type="checkbox" name="pressure" id="pres">
Pressure<br><br>
<input type="submit">
</form>
<script>
function changeFields() {
if(document.getElementById("startdate").disabled == false){
document.getElementById("startdate").disabled = true;
document.getElementById("starttime").disabled = true;
document.getElementById("enddate").disabled = true;
document.getElementById("endtime").disabled = true;
document.getElementById("sametime").disabled = true;
if(document.getElementById("sametime").checked = true){
document.getElementById("sametime").checked = false;
}
document.getElementById("startdate").value = null;
document.getElementById("starttime").value = null;
document.getElementById("enddate").value = null;
document.getElementById("endtime").value = null;
}
else{
document.getElementById("startdate").disabled = false;
document.getElementById("starttime").disabled = false;
document.getElementById("enddate").disabled = false;
document.getElementById("endtime").disabled = false;
document.getElementById("sametime").disabled = false;
}
}
function sameFields() {
if(document.getElementById("enddate").disabled == false){
document.getElementById("enddate").disabled = true;
document.getElementById("endtime").disabled = true;
document.getElementById("enddate").value = null;
document.getElementById("endtime").value = null;
}
else{
document.getElementById("enddate").disabled = false;
document.getElementById("endtime").disabled = false;
}
}
</script>
</body>
</html>
This is the php page fe_page2.php
<?php
require('dbconf.inc');
db_connect();
print"<pre>";
print_r($_POST);
print"</pre>";
$pipelocation = $_POST[pipelocation];
$pipetype = $_POST[Pipe-Type];
$pipeamount = $_POST[amount];
if($pipelocation=='value1' && $pipetype == 'value2' && $pipeamount == 'value3'){
$qrySel="SELECT `Pipe_ID`, `Location`, `Amount`, `PipeType` FROM pipe order by Pipe_ID desc";
$resSel = mysql_query($qrySel);
if($resSel){
while($rowpipe = mysql_fetch_array($resSel, MYSQL_ASSOC)){
echo "Pipe ID:{$rowpipe['Pipe_ID']} <br> ".
"Location: {$rowpipe['Location']} <br> ".
"Amount: {$rowpipe['Amount']} <br>".
"PipeType: {$rowpipe['PipeType']} <br>".
"--------------------------------<br>";
}
echo "Fetched Data Successfully!\n";
}
}
else{
echo"never went in!";
}
db_close();
?>
I have tried different things such as
if(isset($pipelocation) && isset($pipetype) && isset($pipeamount)){
.....
}
and removing the value from html page and using the following piece of code:
if($pipelocation == 'on' && $pipetype == 'on' && $pipeamount == 'on'){
...
}
But still no luck...
Any help would be appreciated.
The code that is presented is purely my work but does include pieces of code that comes from the provided reference below:
https://www.youtube.com/watch?v=LZtXYa9eGGw
You missed the quotations here:
$pipelocation = $_POST['pipelocation'];
$pipetype = $_POST['Pipe-Type'];
$pipeamount = $_POST['amount'];

how to get radio button, checkbox value in csv using ajax and php?

I have a form field and a submit link. i'm generating csv through my php code. the code working fine but the main problem is i'm not able to get checked checkbox, radio button in csv. I'm using ajax and php.
Here is my html code
<div class="form-style" id="contact_form">
<div id="contact_results"></div>
<div id="contact_body">
<input type="radio" id="male" name="gender" required="true" value="Male">
<label for="male"><span></span>M</label>
<input type="radio" id="female" name="gender" required="true" value="Female">
<label for="female"><span></span>F</label>
<input type="radio" id="<18" name="age" required="true" value="<18">
<label for="<18"><span></span><18</label>
<input type="radio" id="18-25" name="age" required="true" value="18-25">
<label for="18-25"><span></span>18-25</label>
<input type="checkbox" id="men_fashion" name="Intrest" required="true" value="Men’s Fashion"/>
<label for="men_fashion"><span></span>Men’s Fashion</label>
<input type="checkbox" id="women_fashion" name="Intrest" required="true" value="Women’s Fashion"/>
<label for="women_fashion"><span></span>Women’s Fashion</label>
<input type="checkbox" id="home_decor" name="Intrest" required="true" value="Home Decor"/>
<label for="home_decor"><span></span>Home Decor</label>
<input type="checkbox" id="gaming" name="Intrest" required="true" value="Gaming"/>
<label for="gaming"><span></span>Gaming</label>
<input type="checkbox" id="toys" name="Intrest" required="true" value="Toys"/>
<label for="toys"><span></span>Toys</label>
<a class="submit"><i class="fa fa-check-circle"></i></a>
</div>
</div>
Here is jquery Ajax
$(document).ready(function() {
$(".submit").click(function() {
var proceed = true;
$("#contact_form input[required=true]").each(function(){
$(this).css('border-color','');
if(!$.trim($(this).val())){
$(this).css('border-color','red');
proceed = false;
}
});
if(proceed) //everything looks good! proceed...
{
//get input field values data to be sent to server
post_data = {
'user_name' : $('input[name=username]').val(),
'Gender' : $('input[name=gender]').val(),
'Age' : $('input[name=age]').val(),
'Intrest' : $('input[name=Intrest').val(),
//'subject' : $('select[name=subject]').val(),
//'msg' : $('textarea[name=message]').val()
};
//Ajax post data to server
$.post('post.php', post_data, function(response){
});
}
});
//reset previously set border colors and hide all message on .keyup()
$("#contact_form input[required=true]").keyup(function() {
$(this).css('border-color','');
$("#result").slideUp();
});
});
Here is my php code
<?php
$cvsData = ""; //Initialize it as empty
if(isset($_POST['Intrest'])) {
$name = $_POST['Intrest'];
foreach ($name as $color){
$cvsData .= $color. " ". " "; //Append the new color value
}
}
$user_name = $_POST['user_name'];
$Gender = $_POST['Gender'];
$Age = $_POST['Age'];
if(empty($fn) || empty($ln) || empty($phone)){
$message = 'Fill in areas in red!';
$aClass = 'errorClass';
}
$cvsData .= ",". $user_name. "," . $Age . "," .$Gender .",". "\n"; //Append the second line here
//Add header here
$exists = true;
if (!is_readable("form.csv")) {
$exists = false;
}
$fp = fopen("form.csv","a");
if($fp){
if (!$exists) {
fwrite("This is my awesome header\n");
}
fwrite($fp,$cvsData);
fclose($fp);
}
?>
Please help me out .
Thanks in Advance.
You just need the :checked selector. Like so:
'Gender' : $('input[name=gender]:checked').val(),
I did changes in ajax
'Gender' : $('input[name=gender]:checked').val(),
'Intrest':$('input[type="checkbox"]:checked').map(function(){
return this.value
}).get(),
And the rest of the code is same

Nothing happens on clicking login

I was trying to create a login page, but it doesn't seem to work. When I click Login/Signup, a popup shows. I enter details and click login. Nothing happens. I try to signup, still nothing happens.
My homepage
<HTML>
<HEAD>
<TITLE>Experiment on Social Network</TITLE>
<script src = "jscript.js"></script>
<link rel = "stylesheet" type = "text/css" href = "style.css" />
<script type = "text/javascript">
window.onload = function()
{
document.getElementById("LoginBtn").onClick = sendRequest("login.php");
document.getElementById("SignBtn").onClick = sendRequest("Signup.php");
};
</script>
</HEAD>
<BODY bgcolor = "black" text = "white" alink = "blue" vlink = "cyan">
Home
<div id = "responseOutput">
<a id = "logSign_pop" href = "#logSign">Login or Signup</a>
<div class = "popup">
<h1>Welcome Guest!</h1>
<form action = "#">
<h3>Login Here</h3>
<label for = "user">Username</label>
<br />
<input type = "text" id = "uname" value = "" />
<br />
<label for = "password">Password</label>
<br />
<input type = "password" id = "pass" value = "" />
<br />
<input type="button" id = "LoginBtn" value="Log In" />
<a class="close" href="#close"></a>
</form>
<form action = "#">
<h3>Sign Up</h3>
<label for="firstName">First Name</label>
<br/>
<input type="text" id="firstName" value="" />
<br />
<label for="lastName">Last Name</label>
<br/>
<input type="text" id="lastName" value="" />
<br />
<label for="useName">UserName</label>
<br/>
<input type="text" id="useName" value="" />
<br />
<label for="email">Email</label>
<br/>
<input type="text" id="email" value="" />
<br />
<label for="pass">Password</label>
<br/>
<input type="password" id="pwd" value="" />
<br />
<input type="button" id = "SignBtn" value="Sign Up" />
<a class="close" href="#close"></a>
</form>
</div>
</div>
<br/>
<center>
<h1>Welcome to MaxZeroEdge Network</h1>
</center>
</BODY>
</HTML>
login.php
<?php
$logID = $_POST["uname"];
$pass = $_POST["pass"];
$login = 0;
$con = mysql_connect("localhost","admin","admin");
if (!$con)
{
die('Could not connect to database: '. mysql_error());
}
$db = mysql_select_db("logDat", $con);
if(!$db)
{
create_db();
}
$tb = "SELECT * from logDat";
$av = #mysql_query($tb);
if(!$av)
{
echo "Error creating database: " . mysql_error();
}
else
{
checkLogin();
}
function checkLogin()
{
//Checks if a particular value exists
$result = mysql_query("SELECT * FROM logDat");
//$passResult = mysql_query("SELECT password FROM logDat");
$check = $logID;
while($login == 0 && $row = mysql_fetch_array($result))
{
if($row["Username"] == $logID)
{
if($row["password"] == $logPass)
{
/*$login = 1;
$expire = 86400*7;
setcookie("user",$logID,$expire);*/
$_SESSION["userid1"] = $logID;
print "<message>Welcome $logID</message>";
//header("Location: Social Network.php"); // redirects
}
else echo "Incorrect Password.";
}
else echo "User doesn't exist.";
}
}
//Runs for the first time only. Creates the database
function create_db()
{
if (mysql_query("CREATE DATABASE logDat",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
}
//Runs for the first time only. Creates the table.
function create_table()
{
//Password varchar(20),
$user = "CREATE TABLE logDat
(
personID int NOT NULL AUTO_INCREMENT,
PRIMARYKEY(personID),
Username varchar(20),
FirstName varchar(50),
LastName varchar(50),
Email varchar(100),
password varchar(20);
)";
mysql_query("INSERT INTO logDat(PRIMARYKEY, Username, FirstName, LastName, Email, password) VALUES(0, 'zero', 'Palash', 'Max', 'maxzeroedge#gmail.com', 'hellfire') ");
}
mysql_close($con);
?>
Remove javascript window.onload function
Use input type submit for button and add targated page to action
As:
<form action = "login.php" method='post'>
<input type="submit" id = "LoginBtn" value="Log In" />
Instead of
<form action = "#">
<input type="button" id = "LoginBtn" value="Log In" />
Same thing for Sugn Up portion

display alert when form is submitted

I need to display the alert saying thanx your msg is received but this doesnot works so please hel me out how to do.Is there coding error or what i coudnt get to it.i would pe pleased if someone helps me out.thankyou
<h4>Send Your Feedback Here.<p>
<form name ="register" action="contact.php" method="post" enctype="multipart/form-data">
Name<br>
<input type="text" name="name" size="30">
</p>
<p>
Email<br>
<input type="text" name="email" size="30"></p><p>
Message<br>
<textarea name="message" rows="5" cols="40"></textarea>
<p>
</p>
<p>
Security Check<br>
Sum of <?php $a = rand(0,9);
$b = rand(0,9);
echo $a . "+" . $b ;
$result = $a + $b ;
?> =
<input type="text" name="ver" /><input type="hidden" name="rval" value="<?php echo $result; ?>" />
<button type="button" onClick="validate()">Send</button>
</p>
</form>
<script type="text/javascript">
function validate()
{
var x = document.register.ver.value;
var y = document.register.rval.value;
var nameCheck = document.register.name.value,
emailCheck = document.register.email.value,
msgCheck = document.register.message.value;
var msg ="";
if(x!=y)
{
msg+= "Sorry!! Captcha Mismached."+"\n";
}
if(nameCheck=="")
{
msg+= "Dont you have a name????"+"\n";
}
if(emailCheck=="")
{
msg+= "Enter email... how am I supposed to contact you"+"\n";
}
if(msgCheck="")
{
msg+= "Dont you have any messages for me??"+"\n";
}
if(msg!="")
{
alert(msg);
}
else
{
<?php
//Get message data.
$name = $_POST['name'];
$message = $_POST['message'];
$email=$_POST['email'];
$ver = $_POST['ver'];
$rval = $_POST['rval'];
if($ver==$rval)
{
//Email it.
mail(
'example#example.com', //Where to send
"Contact form - $name", //Email subject
$message, //Message body
$email //email address
);
echo "<script type=\"text/javascript\">alert('Thank you form is submitted');</script>";
}
else
{
echo "<script type=\"text/javascript\">alert('Wrong captcha');</script>";
}
?>
}
}
You can't mix JavaScript and PHP like that. The PHP will be parsed on the served before the page is served up to the user. Including it inside of your JavaScript else block will have no effect, since the code will be run when the page is generated anyway.
You should look into the use of AJAX.
<script type="text/javascript">
function thevalidate()
{
var x = document.register.ver.value;
var y = document.register.rval.value;
var nameCheck = document.register.name.value,
emailCheck = document.register.email.value,
msgCheck = document.register.message.value;
var msg ="";
if(x!=y) {
msg+= "Sorry!! Captcha Mismached."+"\n";
}
if(nameCheck=="") {
msg+= "Dont you have a name????"+"\n";
}
if(emailCheck=="") {
msg+= "Enter email... how am I supposed to contact you"+"\n";
}
if(msgCheck="") {
msg+= "Dont you have any messages for me??"+"\n";
}
if(msg!="") {
alert(msg);
} else {
<?php
//You need to process data here..
?>
}
}
</script>
<h4>Send Your Feedback Here.<p>
<form name ="register" action="php/process.php" method="post" enctype="multipart/form-data">
Name<br>
<input type="text" name="name" size="30">
</p>
<p>
Email<br>
<input type="text" name="email" size="30"></p><p>
Message<br>
<textarea name="message" rows="5" cols="40"></textarea>
<p>
</p>
<p>
Security Check<br>
Sum of <?php $a = rand(0,9);
$b = rand(0,9);
echo $a . "+" . $b ;
$result = $a + $b ;
?> =
<input type="text" name="ver" /><input type="hidden" name="rval" value="<?php echo $result; ?>" />
<button type="button" onClick="thevalidate()">Send</button>
</p>
</form>

Categories