PHP - Show different html if variable is zero or not - php

Been searching around here but where only able to find to show/hide if the variable isset or not.
I want to show two different infos depending if the variable is zero or not.
If variable is zero the text input and submit button should be visible.
If variable is not zero the facebook variable should be echoed and the reset button showed.
What am i doing wrong?
Heres my code ive been trying:
<?php
$sql2 = $db->query( "SELECT facebook FROM ".$db->prefix."users WHERE id = ".$id);
while ( $row = mysql_fetch_assoc ( $sql2 ) ) {
if (empty($row)) {
?>
<input type="hidden" name="form_sent" value="1" />
<input type="text" name="form[facebook]" value="<?php echo pun_htmlspecialchars($user['facebook']) ?>" size="40" maxlength="50" />
<input type="submit" name="update" value="<?php echo $lang_common['Submit'] ?>" />
<?php
} else {
?>
<?php echo pun_htmlspecialchars($user['facebook']) ?>
<a href="reset.php">
<input type="button" value="Request reset" />
</a>
<?php
}
}
?>
FIX EDIT
I just did it this way and it works fine.
<?php
if($user['facebook'] == '0'){
echo '<input type="button" value="Submit" />';} ?>
<?php
if($user["facebook"] > 0){
echo '<input type="button" value="Request reset" />'; }?>
Now why cant i add this in the echo?
<input type="text" name="form[facebook]" value="<?php echo
pun_htmlspecialchars($user['facebook']) ?>" size="40"
maxlength="50" />
I guess its because php echoes php?
Is there a workaround for this?

try this
<?php
$sql2 = $db->query( "SELECT facebook FROM ".$db->prefix."users WHERE id =
".$id);
while ( $row = mysql_fetch_assoc ( $sql2 ) ) {
if ( $row['facebook'] == 0 ) {
?>
<input type="hidden" name="form_sent" value="1" />
<input type="text" name="form[facebook]" value="<?php echo
pun_htmlspecialchars($user['facebook']) ?>" size="40"
maxlength="50" />
<input type="submit" name="update" value="<?php echo
$lang_common['Submit'] ?>" />
<?php
} else {
echo pun_htmlspecialchars($user['facebook'])
?>
<a href="reset.php">
<input type="button" value="Request reset" />
</a>
<?php
}
}
?>

I just did it this way and it works fine.
<?php
if($user['facebook'] == '0'){
echo '<input type="button" value="Submit" />';} ?>
<?php
if($user["facebook"] > 0){
echo '<input type="button" value="Request reset" />'; }?>
Now why cant i add this in the echo?
<input type="text" name="form[facebook]" value="<?php echo
pun_htmlspecialchars($user['facebook']) ?>" size="40"
maxlength="50" />
I guess its because php echoes php?
Is there a workaround for this?

Related

submit form with one submit button inside while loop with array of inputs

I am trying to submit multiple inputs with data using one submit button with an ajax request. However the the response returns an empty array.
What could be the problem?
jQuery
$('#setup').click(function(){
var formData = new FormData(jQuery('#form8')[0]);
xhr_post(formData,'./ajax/SendUsers.php','SVRMSG');
});
PHP and HTML:
echo '<form id="form8" method="POST">';
$gen = generateRandomString();
include('connection.php');
$stmt = $conn->prepare('SELECT * FROM `clients_table`,`client_education_system` WHERE id = edu_client_id ORDER BY id ASC');
$stmt->execute();
$result = $stmt->get_result();
$num_rows = mysqli_num_rows($result);
while ($row = $result->fetch_assoc()) {
if ($row['edu_course_D'] == 6 ){
?>
ID: <input style="border:0;" type="text" disabled="disabled" name="id[]" value="<?php echo $row['id']; ?>"/>
<br>
Valid Until : <input style="border:0;" disabled="disabled" type="text" name="valid[]" placeholder="validity date" value="<?php echo getLinks('24'); ?>"/>
<br>
Conference date: <input style="border:0;" disabled="disabled" type="text" name="cdate[]" placeholder="conference date" value="<?php echo getContent('24'); ?>"/>
<br>
Serial number : <input style="border:0;" disabled="disabled" type="text" name="serial[]" placeholder="serial" value="<?php echo $row['id'].$gen; ?>"/>
<br>
<input type="text" name="" value=""/>
<br>
<?php
echo '<center>';
echo '<button id="cancelSetup" style="background:white;color:red;" type="button" name="cancelsu">Cancel</button> ';
echo '<button id="setup" style="background:white;color:red;" type="button" >Send</button>';
echo '<div id="SVRMSG"></div>';
echo "</form>";
?>
sendusers.php file
<?php
require_once('..\inc\backend-func.php');
if( $_SERVER['REQUEST_METHOD'] == 'POST'){
print_r($_POST);
}
?>

PHP CMS Trouble

I am in the middle of creating a CMS in PHP & MySQL for the company I work for and have hit a bump in the road.
So I have a table that fetches and lists everything from a table, then next to each record is an edit button.
This button has the following code:
<?php
echo '<td><form method="POST" action="editRecord.php?id='.$id.'">
<input type="submit" name="Edit"></form></td>';
?>
Which then obviously goes to that page, however on the next page i have the following code and it's not running anything.
I don't understand why it's not fetching the information from the database if I have given it the ID...
include 'login/connection.php';
$id = $row['id'];
$result = $db->query("SELECT * FROM vehicleOrderForm WHERE id = ' $id ' ");
$row = mysqli_fetch_array($result);
That code is at the start and then further down is this;
<?php
echo $row["id"];
$id = $row["id"];
$make = $row["make"];
$varient = $row["varient"];
$stockno = $row["stockno"];
$transmission = $row["transmission"];
$cc = $row["cc"];
$colour = $row["colour"];
$delivery = $row["delivery"];
$stock = $row["stock"];
$sold = $row["sold"];
$customer = $row["customer"];
$tax = $row["tax"];
$comments = $row["comments"];
?>
<div>
<h1><?php echo $id; ?></h1>
</div>
<form method="POST" action="">
<p>Make:</p>
<?php echo '<input type="text" name="make" value="'.$make.'" />;' ?>
<p>Varient:</p>
<input type="text" name="varient" value=" <?php echo $varient; ?>" /> <br />
<p>Stock Number:</p>
<input type="email" name="stockno" value="<?php echo $stockno; ?>" /> <br />
<p>Transmission:</p>
<input type="number" name="transmission" value="<?php echo $transmission; ?>" /> <br />';
<p>CC:</p>
<input type="username" name="cc" value="<?php echo $cc; ?>" /> <br />';
<p>Colour:</p>
<input type="text" name="colour" value="<?php echo $colour; ?>" /> <br />';
<p>Expected Delivery:</p>
<input type="text" name="delivery" value="<?php echo $delivery; ?>"/> <br />';
<p>In Stock:</p>
<input type="text" name="stock" value="<?php echo $stock; ?>"/> <br />';
<p>Status:</p>
<input type="text" name="sold" value="<?php echo $sold; ?>"/> <br />';
<p>Customer:</p>
<input type="text" name="customer" value="<?php echo $customer; ?>"/> <br />';
<p>Tax:</p>
<input type="text" name="tax" value="<?php echo $tax; ?>"/> <br />';
<p>Comments:</p>
<input type="text" name="comments" value="<?php echo $comments; ?>"/> <br />';
<input type="Submit" name="Edit" value="Edit">';
</form>
If anyone can understand it, that would be great.
That form around the Edit button looks like it's totally useless.. You can't sent the form with POST method, and attach an id to the action url: that param won't be sent.
Solution 1: If you can, I would suggest you to switch to GET param and use an anchor. Like this:
<?php echo '<td>Edit</td>'; ?>
And edit your code on page editRecord.php as follows:
$id = $_GET['id']; // you need to receive the param from the url
Solution 2: If you want to keep the <form>, you should write the following code:
<?php
echo '<td><form method="POST" action="editRecord.php"> '.
'<input type="hidden" name="id" value="'.$id.'" />'.
'<input type="submit" name="Edit"></form></td>';
?>
And on page editRecord.php:
$id = $_POST['id']; // you need to receive the param from the url

Passing form inputs from different pages to submit at final page

I have 4 different pages both with one form each.
I want to gather all the entries on each of the pages and submit once.
Here is code.
Page 1
<form action="page2" method="POST">
<input type="text" name="sex">
<input type="submit" value="Submit">
</form>
Page 2
<form action="page3" method="POST">
<input type="text" name="size">
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 3
<form action="page4" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="verNote.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" > <input type="submit" value="Submit">
</form>
Then i will like to get all the infos on verNote.php
<?php
echo $_POST['sex'];
echo '<br>';
echo $_POST['size'];
echo '<br>';
echo $_POST['color'];
echo '<br>';
echo $_POST['likes'];
?>
This code above dont seem to post entries from both pages 1 and 2, just for 3 and 4 alone gets submitted.
Will appreciate immediate assistance form anyone who understands my question.
Regards!
You need to load the hidden fields again each time
Page 3
<form action="B.php" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="B.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
I didn't understand 100% what you're trying to achieve, but have you tried using sessions?
Do this in B.php:
<?php
session_start();
if( isset($_POST['sex']))
$_SESSION['sex'] = $_POST['sex'];
if( isset($_POST['size']))
$_SESSION['size'] = $_POST['size'];
if( isset($_POST['color']))
$_SESSION['color'] = $_POST['color'];
if( isset($_POST['likes']))
$_SESSION['likes'] = $_POST['likes'];
?>
Then you can retrieve the values from any other file, just call session_start(); and use the $_SESSION superglobal.
EDIT
Using sessions, you verNote.php file could be something like this:
<?php
session_start();
echo $_SESSION['sex'];
echo '<br />';
echo $_SESSION['size'];
echo '<br />';
echo $_SESSION['color'];
echo '<br />';
echo $_SESSION['likes'];
echo '<br />';
?>

MySql query returns multiple results, on update everything gets updated, but I only need one of them updated

I know, that my problem lies in that in my where statement there is no unique identifier which row should be updated only. Here is what I have just to make it more clear:
<?php
$res=mysql_query("select * from orders where feedback_reminder='1' ");
while($row=mysql_fetch_array($res))
{ ?>
<form method="post">
<div><input name="ship_date" type="text" id="ship_date" value="<?php echo $row['ship_date']; ?>" /></div>
<div><input name="feedback_reminder" type="text" id="feedback_reminder" value="<?php echo $row['feedback_reminder']; ?>" /></div>
<div><input type="submit" name="button" id="button" value="Update" class="submit" onMouseOver="this.className='submit submithov'" onMouseOut="this.className='submit'"/></div>
</form>
<?php
if(count($_POST)>0)
{
mysql_query("update orders set
ship_date='".$_POST["ship_date"]."',
feedback_reminder='".$_POST["feedback_reminder"]."'
where id='".$row['id']."' ");
}
}
?>
It is obvious that this doesn't work, because where id='".$row['id']."' is true for all of the rows in the orders table and this is why my whole table gets constantly updated. How do I make this work so that only the row gets updated where I have pressed the Update button? I hope I was clear enough, thanks for heliping!
do it with hidden field
$res=mysql_query("select * from orders where feedback_reminder='1' ");
while($row=mysql_fetch_array($res))
{ ?>
<form method="post">
<div><input name="ship_date" type="text" id="ship_date" value="<?php echo $row['ship_date']; ?>" /></div>
<div><input name="feedback_reminder" type="text" id="feedback_reminder" value="<?php echo $row['feedback_reminder']; ?>" /></div>
<input name="id" type="hidden" value="<?php echo $row['id']; ?>" />
<div><input type="submit" name="button" id="button" value="Update" class="submit" onMouseOver="this.className='submit submithov'" onMouseOut="this.className='submit'"/></div>
</form>
<?php
if(isset($_POST['button']))
{
mysql_query("update orders set
ship_date='".$_POST["ship_date"]."',
feedback_reminder='".$_POST["feedback_reminder"]."'
where id='".$_POST['id']."' ");
}
}
use redirect after updating record.

Not validating one of the search fields

I want to check if either of my search fields are empty, and if they are i write out a failure message. For some reason the following code doesn't work when I try to submit empty values in both search fields, or leaving the q2 search field empty. It only writes out the failure message when I only insert a value into the first field. Hope you guys can help.
<div id="searchField">
<form name="searchField" action="" method="get">
<input type="search" name="q" id="submitQuery" autofocus="autofocus" value="<?= $_GET['q']; ?>" placeholder="enter one movie"/>
<input type="search" name="q2" id="submitQuery2" value="<?= $_GET['q2']; ?>" placeholder="and then another one"/>
<input type="hidden" id="v" name="v" value="2" />
<input type="hidden" id="b" name="b" value="" />
<button type="submit" id="loop" style="border:0px; background:transparent; vertical-align:middle; margin-left:-85px; margin-top:-3px;">
<img src="images/loop.png" width="75" height="75" alt="submit" />
</button>
</form>
</div>
<div id="theData">
<?
if(str_replace(" ", "",$_GET['q'])!="" && str_replace(" ","",$_GET['q2'])!=""){
$qContent = $_GET['q'];
$succes = mysql_query("SELECT*FROM film WHERE name='".$qContent."'");
include("searchMaster.php");
if(mysql_num_rows($succes)) {
while($o = mysql_fetch_array($succes)){
echo $o['name'];
if($o['trailer']){
echo '<div id="ytplayerWrap">';
echo '<iframe id="ytplayer" type="text/html" width="640" height="390"
src="http://www.youtube.com/embed/'.$o['trailer'].'?http://amnestic.ueuo.com"
frameborder="0"/>';
echo '</div>';
}else {
echo '<span style="font-size:20px;">';
echo '<br /><br /> No trailer was found for <em>'.$_GET['q'].'</em> :(';
echo '</span>';
}
}
}else{
echo 'You did the fuckup :(';
}
}else{
echo 'You did the fuckup :(';
}
?>
The name and id of your input fields must be same
Like this
<input type="text" name="submitQuery" id="submitQuery" autofocus="autofocus" value="<?= $_GET['q']; ?>" placeholder="enter one movie"/>
<input type="text" name="submitQuery2" id="submitQuery2" value="<?= $_GET['q2']; ?>" placeholder="and then another one"/>

Categories