Change href in another PHP page using a variable - php

I'm brand new to this and trying to scab together two different ideas. Each of them work individually, but I'm trying to integrate them.
I am able to upload and image (which creates a list itme with an href="javascript:void(0).
I can allow user input to assign a url to an existing text link.
what I'm trying to do is target the dynamically created list item in the upload.php page and change the href="javascript:void(0) to the user specified variable from index.php
index.php code
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>image upload</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#images').on('change',function(){
$('#multiple_upload_form').ajaxForm({
target:'#images_preview',
beforeSubmit:function(e){
$('.uploading').show();
},
success:function(e){
$('.uploading').hide();
},
error:function(e){
}
}).submit();
});
});
</script>
</head>
<body>
<div style="margin-top:50px;">
<div class="upload_div" >
<form name="multiple_upload_form" id="multiple_upload_form" enctype="multipart/form-data" action="upload.php">
<input type="hidden" name="image_form_submit" value="1"/>
<label>Choose Image</label>
<input type="file" name="images[]" id="images" multiple >
<div class="uploading none">
<label> </label>
<img src="uploading.gif"/>
</div>
</form>
</div>
<div class="gallery" id="images_preview">
</div>
</div>
<div class="form_div" id="url_input">
<form id="form1">
URL - example http://www.yourdomain.com: <input name="name" type="text" size="20">
</form>
<button onclick="outputname()">Submit</button>
<script>
function outputname() {
var x,y,name,a,b,answer;
x=document.getElementById("form1") ;
y=x.elements["name"].value;
document.getElementById("upload.php#web").setAttribute("href",y);
}
</script>
<p id="demo">
<a id="link">this is a link</a>
</p>
</div>
</body>
</html>
upload.php code
<?php
if($_POST['image_form_submit'] == 1)
{
$images_arr = array();
foreach($_FILES['images']['name'] as $key=>$val){
$image_name = $_FILES['images']['name'][$key];
$tmp_name = $_FILES['images']['tmp_name'][$key];
$size = $_FILES['images']['size'][$key];
$type = $_FILES['images']['type'][$key];
$error = $_FILES['images']['error'][$key];
############ Remove comments if you want to upload and stored images into the "uploads/" folder #############
/*$target_dir = "uploads/";
$target_file = $target_dir.$_FILES['images']['name'][$key];
if(move_uploaded_file($_FILES['images']['tmp_name'][$key],$target_file)){
$images_arr[] = $target_file;
}*/
//display images without stored
$extra_info = getimagesize($_FILES['images']['tmp_name'][$key]);
$images_arr[] = "data:" . $extra_info["mime"] . ";base64," . base64_encode(file_get_contents($_FILES['images']['tmp_name'][$key]));
}
//Generate images view
if(!empty($images_arr)){ $count=0;
foreach($images_arr as $image_src){ $count++?>
<ul class="reorder_ul reorder-photos-list">
<li id="image_li_<?php echo $count; ?>" class="ui-sortable-handle">
<img src="<?php echo $image_src; ?>" alt="">
</li>
</ul>
<?php }
}
}
?>
Like I said, I'm brand new to this and there may be a far more elegant solution, but I've gotten this far from just hunting and pecking... thanks in advance for any help.

Related

I want to read a text file and store the data in array with try-catch condition then convert it into format in php?

I want to read the text file and read it in PHP then splits into arrays and showing data in another text file. After that, I have converted this file to format and then any user can download the converted CSV file. I have attached a file herewith File.txt
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6">
<h2>IRIS to ASB Conversion</h2>
<div class="panel panel-default">
<div class="panel-body">
Please Upload IRIS File <br/>
<br/> <br/>
<form action="convertfile1.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" > <br/><br/><input type="submit" class="btn btn-info " value="Generate ASB Format">
</form>
</div>
</div>
</div>
</body>
</html>
<?php
$file="iris.txt";
$fopen = fopen($file, 'r');
$fread = fread($fopen,filesize($file));
fclose($fopen);
$remove = "\n";
$split = explode($remove, $fread);
$array[] = null;
$tab = ",";
foreach ($split as $string)
{
$row = explode($tab, $string);
array_push($array,$row);
}
echo "<pre>";
print_r($array);
echo "</pre>";
?>
My code is not showing anything in an array, it is blank. How could I get data in different rows in an array with the try-catch pattern in PHP? After that file will be converted and download by any user as.CSV file in the end.
Please help me out.

Input type text is not receiving in php file on submit

I want to send title of the image on submit of form but its not receiving in upload.php i don't know the reason only undefined variable warning is displayed. help anybody.
multiupload.php
<!DOCTYPE html>
<html>
<head>
<title>Upload Multiple Images Using jquery and PHP</title>
<!-------Including jQuery from google------>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="script.js"></script>
<!-------Including CSS File------>
<link rel="stylesheet" type="text/css" href="style.css">
<body>
<div id="maindiv">
<div id="formdiv">
<h2>Multiple Image Upload Form</h2>
<form enctype="multipart/form-data" action="" method="post">
First Field is Compulsory. Only JPEG,PNG,JPG Type Image Uploaded. Image Size Should Be Less Than 100KB.
<hr/>
<input type="text" name="title" />
<div id="filediv"><input name="file[]" type="file" id="file"/></div><br/>
<input type="button" id="add_more" class="upload" value="Add More Files"/>
<input type="submit" value="Upload File" name="submit" id="upload" class="upload"/>
</form>
<br/>
<br/>
<!-------Including PHP Script here------>
<?php include "upload.php"; ?>
</div>
<!-- Right side div -->
<div id="formget"><a href=http://www.formget.com/app><img src="formget.jpg" alt="Online Form Builder"/></a>
</div>
</div>
</body>
</html>
upload.php
<?php
if (isset($_POST['submit'])) {
echo 'hell';
$j = 0; //Variable for indexing uploaded image
$value = isset($_POST['title']) ? $_POST['title'] : '';
echo 'title:'.$value;
$target_path = "uploads/"; //Declaring Path for uploaded images
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array
$ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.)
$file_extension = end($ext); //store extensions in the variable
if( move_uploaded_file($_FILES["file"]["tmp_name"][$i], $target_path . $_FILES["file"]["name"][$i])){
echo ($i+1).')'. $_FILES["file"]["name"][$i]. '.<span id="noerror">is uploaded successfully!.</span><br/><br/>';
} else {//if file was not moved.
echo $i. ').<span id="error">please try again!.</span><br/><br/>';
}
}
}
?>
error/warning:
( ! ) Notice: Undefined index: title in C:\wamp\www\multiple_image_upload\upload.php on line 5
Two reason for this
1)you forget input type for title in your form
<input type="text" name="title" />
2)May be instead of $_POST['title'] it is
$_POST['tender']
in your upload.php

session variable won't display

I am trying to send POST data into a SESSION variable
$_SESSION['plan'] = $_POST['plan']
info.php show that sessions are loaded.
browser -> inspect element appears to show sessions initialized but no key => values.
But I cannot get the session value to display.
no errors in apache logs.
main dynamic frame:
cat index.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta name="robots" content="index,follow"/>
<link rel="icon" href="images/favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<nav class="nav">
<ul>
BLAH
</ul>
</nav>
<div id="content">
<?php
$pages_dir = 'pages';
if (!empty($_GET['p'])) {
$pages = scandir($pages_dir, 0);
unset($pages[0], $pages[1]);
$p=$_GET['p'];
if(in_array($p.'.inc.php', $pages)){
include($pages_dir.'/'.$p.'.inc.php');
}else {
echo 'Sorry, page not found.';
}
}else{
include($pages_dir.'/home.inc.php');
}
?>
</div>
</body>
</html>
User form page:
cat pages/payment.inc.php
<?php
//$_SESSION['plan'] = $_POST['plan'];
?>
<div id="content_pay">
<form action="pages/scheckout.php" method="post">
<div>
<input type="radio" id="plan1" name="plan" value="2500"> Beta membership <br><br>
<input type="radio" id="plan2" name="plan" value="3500"> VIP membership <br><br>
<label for="plan"> If you would like to pay another amount, enter the amount here:</label>
<input type="text" id="plan3" name="plan" />
<br>
<label for="invoice_num"> Enter the invoice number here:</label>
<input type="text" name="invoice_num" /> <br>
<input type="submit" value="submit" name="submit">
</div>
</form>
</div>
Basically this page is here just to capture the POST and assign it to SESSION then redirect with SESSION loaded:
cat pages/scheckout.php
<?php
session_start();
$_SESSION['plan'] = $_POST['plan'];
//needed to prevent weird race conditions
session_write_close();
header("location: ../index.php?p=scheckout");
die();
echo "<br>";
echo $_SESSION['plan'];
?>
Where is goes to:
cat pages/scheckout.inc.php
<?php
require_once('pages/sconfig.php');
?>
<div id="content_pay">
<form action="pages/scharge.php" method="post">
<div>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
myElem.setAttribute('data-amount', <?php $_SESSION['plan']; ?>);
myElem.setAttribute('data-description', <?php $_SESSION['plan']; ?>); >
</script>
</div>
</form>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<?php
echo $_SESSION['plan'];
?>
How do I get SESSIONS loaded from POST to display?
You have to start the session only once, before the html output. A session stays open until you use session_destroy or unset the $_SESSION variable, or close the browser. I suggest you, to delete all session_starts except the one in the index.php.
you use SESSION['plan'], but should use echo SESSION['plan']
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
myElem.setAttribute('data-amount', <?php echo($_SESSION['plan']); ?>);
myElem.setAttribute('data-description', <?php echo($_SESSION['plan']); ?>); >
</script>

How do I check which input button has been pressed in PHP?

so I'm new to php and I have two buttons on this html page here (the id value is included in the url):
<!DOCTYPE html>
<head>
<title>StoryBlox is a Social Story Builder Tool</title>
<meta charset="utf-8">
<!-- these support the header/footer formatting -->
<link type="text/css" rel="stylesheet" href="css/main.css">
<link type="text/css" rel="stylesheet" href="css/header_footer.css">
<script src="js/header.js"></script>
<?php //include_once 'confirm_login.php'
include_once 'story_manager.php';
include_once 'open_connection.php';
//include_once 'functions.php';
//sec_session_start();
if(isset($_GET['id'])){
$str_id = $_GET['id'];
$draft_id = get_story_attribute($str_id, 'draft');
}else{
echo "Invalid story id.";
echo "<br>";
}
?>
</head>
<body>
<div id="wrapper_main">
<div id="wrapper_content">
<?php include_once 'header.php'; ?>
<h1>Welcome to StoryBlox Create Story!</h1>
</div>
<!-- menu -->
<!--<div id="inputs"> -->
<form id="create_form" action="save_story.php?id=<?php echo $str_id?>" method="POST">
<input type="text" name="storyTitle" id="title" placeholder="Enter title." autofocus/><br>
<textarea rows="4" cols="50" name="storyDesc" id="description" placeholder="Enter description here."></textarea>
<div id="footer">
<input type="button" name="draftBtn" onclick="this.form.submit()" value="Save as Draft"/>
<input type="button" name="finalBtn" onclick="this.form.submit()" value="Finished!"/>
</div>
</form>
</div>
</div>
<?php include_once 'footer.php'; ?>
</body>
When I click one of these two buttons, I'm brought to this php document here:
include_once 'open_connection.php';
include_once 'story_manager.php';
$mysqli = open_connection();
if($_SERVER['REQUEST_METHOD'] === 'POST'){
if(isset($_POST['draftBtn'])){
$title = $_POST['storyTitle'];
$desc = $_POST['storyDesc'];
$str_id = $_GET['id'];
update_story_title($str_id, $title);
//update_story_description($str_id, $desc);
header('Location: createStory.php');
}
elseif(isset($_POST['finalBtn'])){
$title = $_POST['storyTitle'];
$desc = $_POST['storyDesc'];
$str_id = $_POST['storyID'];
update_story_title($str_id, $title);
//update_story_description($str_id, $desc);
save_draft_as_completed($str_id);
header('Location: ../home.php');
}else{ echo "failed";}
}?>
And I always get "failed" printed out on my page. I've been Googling this for hours and I don't understand where I'm going wrong here. If anybody could help that would be appreciated. Also, if anyone could shed some light on what the equivalent to
<input type="textarea">
would be that would be great. Thanks!
Use
<input type="submit" name="draftBtn" value="Save as Draft"/>
instead of the button types with their onclick events.
try to use submit tag instead of button.
and if you want to use button tag then you can pass value through hidden field. set value of hidden field on click event.

Imageshack API almost working

This tutorial http://www.blocsoft.com/blog/imageshack.asp shows a great way to upload to imageshack and get a image url back although its for ASP and I need it in PHP.
Any ideas if and how this could be done?
Thanks heaps.
I just rewrote the asp to php, no clue if it works haven't tested it, but if the asp does work then the php should also.
index.php
<html>
<head>
<title>AJAX image upload</title>
</head>
<body>
<form action="http://imageshack.us/redirect_api.php" target="AXframe" method="post" enctype="multipart/form-data">
<input type="file" name="media"/>
<input type="hidden" name="key" value="YOUR_DEVELOPER_KEY">
<input type="hidden" name="error_url" value="http://example.com/error.php">
<input type="hidden" name="success_url" value="http://example.com/success.php?one=%y&two=%u&three=%s&four=%b&five=%i">
<input type="submit"/>
</form>
<iframe style="visibility:hidden" id="AXframe" name="AXframe"></iframe>
<div id="link"></div>
<div id="yfrog"></div>
<div id="image"></div>
</body>
</html>
success.php
<?php
$str1 = $_GET["one"];
$str2 = $_GET["two"];
$str3 = $_GET["three"];
$str4 = $_GET["four"];
$str5 = $_GET["five"];
?>
<script type="text/javascript">
parent.document.getElementById('yfrog').innerHTML = '<?php echo($str1); ?>';
parent.document.getElementById('link').innerHTML = '<?php echo($str2); ?>';
parent.document.getElementById('image').innerHTML = '<img src="http://img<?php echo($str3); ?>.imageshack.us/img<?php echo($str3); ?>/<?php echo($str4); ?>/<?php echo($str5); ?>">';
</script>
error.php
<script type="text/javascript">
alert("There was an error uploading the file.");
</script>

Categories