Trying to Update my database (Laravel) with some javascript - javascript

The code is working just fine on my local machine, but when I upload it to the server it doesn't work.
This is my back end code:
public function changeCodePost(Request $request)
{
$s = Setting::all()->first();
$s->update([
'code' => $request->scriptCode
]);
alert()->success('تم الحفظ');
return redirect()->back();
}
And here is the form:
<form method="post" action="{{route('code.post')}}">
#csrf
<div class="form-group">
<input type="text" name="scriptCode" class="form-control" value="{{$code}}">
</div>
#error('scriptCode')
{{$message}}
#enderror
<button class="btn btn-primary">حفظ</button>
</form>
Please help, could it be the server I'm uploading on?
I want to save a value such as:
but only when I enter a value like <script></script> it doesn't work.

Related

JQuery AJAX - passing data from form to PHP script

My task is triggering php script from HTML form, which is updating JSON file from which HTML is generated. Here is how i am trying to do that.
HTML Form:
<form method="post">
<label for="board">Board to update:</label><br>
<input type="text" id="board" name="board"><br><br>
<input type="button" value="Submit" onclick="update()">
</form>
Javascript:
function update() {
var board=$('#board').val();
$.post('update.php',{boardname:board}, function() {
alert("update successfull");
})
}
and PHP:
<?php
$q=$_POST['boardname'];
echo ($q);
shell_exec('curl https://cyberland.club/' . $q . '/?num=1000 > posts.json');
?>
There is no error after clicking button, but website is not updating. Can you tell me why is that? What am i doing wrong? I was using this tutorial https://www.youtube.com/watch?v=jVAaxkbmCts

How use Vuesax upload component in Laravel/Vue.js project?

I'm working on this project base on tutorials around web and YOUTUBE .
My problem is that base on tutorial I've got a from like this:
<form class="form-horizontal">
<div class="form-group">
<label for="mellicode_front_url" class="col-sm-2 control-label">scan</label>
<div class="col-sm-12">
<input type="file" #change="updateMelliCodeFrontScan" name="mellicode_front_url" class="form-input" >
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-12">
<button #click.prevent="saveMelliCodeFrontScan" type="submit" class="btn btn-success">submit scan</button>
</div>
</div>
</form>
as u can see in this code when i opened a IMAGE file i use #change method, then I'll use click.prevent in submit button to upload image into host.
problem is I'm using component from this site :
https://lusaxweb.github.io/vuesax/components/upload.html
which it's using this type of code for uploading :
<vs-upload action="https://jsonplaceholder.typicode.com/posts/" #on-success="successUpload" />
I've changed and removed action with onChange and other things but just because i'm beginner i'm sure i didn't properly. so what i'm trying to do is implement those #change="updateShenasnameScan" & #click.prevent="saveShenasnameScan" into this vs-upload component.
I think that component not supporting the binding or form object to assign the files to it.
But in the backend in Laravel you can get files by $_FILES, this will gives you an object of files that you can use it and store the files simply on the server.
Here is my work on this upload system
Laravel Function
public function store(Request $request)
{
$stored = [];
$files = $_FILES;
foreach ($files['archive']['tmp_name'] as $key => $value) {
$file = file_get_contents($value);
if(Storage::disk('local')->put('archive/'. $files['archive']['name'][$key], $file)){
$stored[$key] = 'archive/'. $files['archive']['name'][$key];
}else{
$stored[$key] = $files['archive']['error'][$key];
}
}
return $stored;
}
And the VUE code, that you can find more about it here https://lusaxweb.github.io/vuesax/components/upload.html#automatic
<vs-upload multiple fileName='archive[]' automatic action="/api/archive" #on-success="successUpload" />
And the $_FILES data as bellow

php file upload to the database by update query

I am trying to update an upload image to folder using php file upload and could not get the previously occured image in the database. Here is my code,problem is I could not get the previous image in the file upload section from database. Please help.
if(isset($_GET['msg']))
{
echo $_GET['msg'];
}
$id=(isset($_GET['id']))?$_GET['id']:'';
$val=view_data1_event($id);
if (isset($_POST["events"])&&isset($_POST["description"]))
{
$id=$_POST["id"];
$title=$_POST["events"];
$description=$_POST["description"];
$filetmp=$_FILES["images"]["tmp_name"];
$filename=$_FILES["images"]["name"];
$filetype=$_FILES["images"]["type"];
$filepath= "photo2/".$filename;
move_uploaded_file( $filetmp,$filepath);
$update1=update_query($title,$description,$filename,$filepath,$id);
if($update1)
{
echo "<script>location.href='hotel1_galery_event.php'</script>";
}
}
HTML
<form action="hotel2_galery_eventedit.php" method="post" class="col-sm-4" enctype="multipart/form-data">
<div class="form-group has-info">
<label>Event Related images</label>
<input type="file" name="images">
<input type="hidden" name="image_name" value="<?php echo $val->image_name?>">
<input type="hidden" name="image_path" value="<?php echo $val->image_path?>">
</div>
<button type="submit" class="btn btn-primary">
<span>UPDATE</span>
</button>
</form>
Your form method is post and you are using as get in php.
Please replace all GET with POST
if(isset($_GET['msg']))
This should be,
if(isset($_POST['msg']))

How to pass text from form to javascript function

So I have little problem, I want to write something in form and pass it to symfony2 controller, but I have no idea how can I do it. I tried something like this:
Index.html:
<form action='#close' ng-submit='calCtrl.test(day.id, $index)'>
<div class='form-field'>
<span class='sp'>Podaj opis (opcjonalnie):</span><br />
<textarea></textarea>
</div>
<div class='form-field'>
<input type='submit' value='Zmień'>
</div>
</form>
Controllers.js:
this.test = function($id, $index) {
$http.put('http://localhost:8000/api/v1/notes/' + $id + '.json');
};
Symfony controller:
public function putNoteAction(Request $request, $id) {
}
Now I can pass only $id and nothing more.
To get value from form to javascript function, you can do as follows:
<form onsubmit="myFunc()">
<input type="text" id="t1">
<input type="submit">
</form>
<script>
function myFunc()
{
alert(document.getElementById("t1").value);
}
</script>
Furthermore, if the form is submitting value to server side, then still at server end you can assign the values to javascript variables. like:
<form action="action.php" method="GET">
<input type="text" id="t1" name="t1">
<input type="submit">
</form>
action.php code will be as follows:-
// To assign form posted value to javascript variable.
var javascript_variable = <?php echo $_GET['t1']; ?>;
You can use same approach with symfony using symfony syntax..

Sending HTML form via AJAX to PHP server

I'm working on phonegap, basically its like making mobileapps crossplatform by using HTML, JS and CSS. On the device i currently have the JS and the HTML (form) in same document.
What I'm trying to do is to pass email and password to my server, and then process it there through a login. I've tested the login script on the server and it works with hardcoded data. So I'm guessing somewhere when sending the data from the device its failing.. I'm fairly new to JS too.
I tried to hardcode the data in the AJAX but it doesnt seem to work. Preferebly I would like to use something like var pdata = $('#form').serialize(); or something else if its better.
Any ideas?
EDIT: Forgot to say that the PHP on the server auto submits by using JS when $_POST is set (isset)
The form
<form id="form" onsubmit="dologin()">
<div class="form-group">
<label for="email">Epost</label>
<input type="email" class="form-control" name="email" value="" placeholder="Epost">
</div>
<div class="form-group">
<label for="password">Passord</label>
<input type="password" class="form-control" name="password" value="" placeholder="Passord">
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="remember_me">
Husk meg
</label>
</div>
<button type="submit" class="btn btn-primary">Logg inn</button>
</form>
The javascript
<script>
function dologin() {
//var pdata = $('#form').serialize();
//alert(pdata);
$.ajax({
type: 'POST',
data: {email:"test#test.no",password:"test"},
url: 'LINK',
success: function(data) {
alert(data);
},
error: function() {
alert("error");
}
});
return false;
};
</script>
The PHP
<form id="form" method="post">
<!-- {{ Form::label('email', 'Email Address') }} -->
<div class="form-group">
<input type="text" name="email" value="<?php if(isset($_POST["email"])) echo $_POST['email'];?>">
</div>
<div class="form-group">
<!-- {{ Form::label('password', 'Password') }} -->
<input type="text" name="password" value="<?php if(isset($_POST["password"])) echo $_POST['password'];?>">
</div>
</form>
Are you able to hit your server via through phonegap?
If no then please check your config.xml for white list urls - change access control property to
access origin = "*"
Hopeful you will be able to hit your server with data.
You can use weinre to debug your app. That way you will be able to see if the request was placed from the app or not.
http://people.apache.org/~pmuellr/weinre/docs/latest/Home.html

Categories

Resources